字串編碼C#

來源:互聯網
上載者:User

標籤:包括   i++   har   例子   字元替換   替換   substr   div   int   

給定一個字串,請你將字串重新編碼,將連續的字元替換成“連續出現的個數+字元”。比如字串AAAABCCDAA會被編碼成4A1B2C1D2A。 

輸入描述:

每個測試輸入包含1個測試案例每個測試案例輸入只有一行字串,字串只包括大寫英文字母,長度不超過10000。

輸出描述:

輸出編碼後的字串

輸入例子:

AAAABCCDAA

輸出例子:

4A1B2C1D2A

牛客網頁運行代碼如下:

using System;public class Program{    public static void Main(){         string input = System.Console.ReadLine();            int inputLength = input.Length;            int keyCount = 0;            char keyValue;            char[] c = new char[inputLength];            for (int i = 0; i < inputLength; i++)            {                c[i] = Convert.ToChar(input.Substring(i, 1));            }            for (int i = 0; i < inputLength; )            {                keyValue = c[i];                while (i != inputLength && keyValue == c[i])                {                    i++;                    keyCount++;                }                Console.Write("{0}{1}", keyCount, keyValue);                keyCount = 0;            }            Console.ReadKey();    }}

VS中的代碼如下:

using System;namespace 字串編碼{    class Program    {        static void Main(string[] args)        {            string input = System.Console.ReadLine();            int inputLength = input.Length;            int keyCount = 0;            char keyValue;            char[] c = new char[inputLength];            for (int i = 0; i < inputLength; i++)            {                c[i] = Convert.ToChar(input.Substring(i, 1));            }            for (int i = 0; i < inputLength; )            {                keyValue = c[i];                while (i != inputLength && keyValue == c[i])                {                    i++;                    keyCount++;                }                Console.Write("{0}{1}", keyCount, keyValue);                keyCount = 0;            }            Console.ReadKey();        }    }}

 歡迎交流。

字串編碼C#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.