Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string s = "aaaabbbddddaaa";
- //s = "abc";
- string output = "";
- char c = s[0];
- int count = 0;
- for (int i = 0; i < s.Length; i++)
- {
- if (c == s[i])
- {
- count++;
- }
- else
- {
- output += count.ToString() + c;
- c = s[i];
- count = 1;
- }
- }
- // include last char
- if (output.Length > 0 && output[output.Length - 1] != c)
- {
- output += count.ToString() + c;
- }
- Console.WriteLine(output); // output: 4a3b4d3a
Add Comment
Please, Sign In to add comment