Contents

[코딩도장] 문자열 압축 cs문제풀이

   Jul 31, 2020     1 min read     - Comments

1. 문제

LV.2

문자열을 입력받아서, 같은 문자가 연속적으로 반복되는 경우 반복 횟수 표시하기

예시
입력: aaabbcccccca

출력: a3b2c6a1

2. 풀이

static void Main()
{
    string input = Console.ReadLine()+'\0';
    string output = null;
    char compare = input[0];
    int count = 0;

    foreach(char n in input)
    {
        if (n != compare)
        {
            output += compare + count.ToString();
            compare = n;
            count = 0;   
        }
        count++;
    }
    Console.WriteLine(output);        
}
foreach 표
 0123456789101112
inputaaabbccccccanull
compareaaaabbcccccca
output   a3 b2     c6a1