-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
45 lines (39 loc) · 1.06 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Text;
namespace CompressString
{
class Program
{
static void Main(string[] args)
{
string toCompress = "aaaabbccccddeefffff";
char[] chars = toCompress.ToCharArray();
char prevChar = new char();
int count = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.Length; i++)
{
if (prevChar != 0)
{
if (prevChar != chars[i])
{
sb.Append(count).Append(prevChar);
prevChar = new char();
count = 1;
}
else
{
count++;
}
}
else
{
prevChar = chars[i];
count++;
}
}
sb.Append(count).Append(prevChar);
Console.WriteLine(sb.ToString());
}
}
}