-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressString.cs
61 lines (50 loc) · 1.95 KB
/
CompressString.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
namespace CompressString
{
class Program
{
public string compress(string inpStr)
{
string retStr = String.Empty;
int step=0;
char curChar;
// int curCount=0;
// bool resume=true;
while(true)
{
int subStep=1;
// current char will be the current step of input string
curChar = inpStr[step];
while(true)
{
bool nextStepIsOutOfLimits = int.Equals(step+subStep, inpStr.Length);
// upon reaching the end (next step is out of limits)
// write the last character, how many times it occurs, and break the loop
if( nextStepIsOutOfLimits )
{
retStr += curChar.ToString() + subStep.ToString();
return retStr;
}
// compare input string's length and current step
// if they're the same break out of the loop
if( int.Equals(step+subStep, inpStr.Length) ) { break; }
// if the current character equals input string's substep'th value
// add one and continue to the next loop
bool charsAreSame = char.Equals(curChar, inpStr[step+subStep]);
if( charsAreSame )
{
subStep++;
continue;
}
// this part is basically the else block.
// adds the current character and the subStep amount
// to the return string
retStr += curChar.ToString() + subStep.ToString();
step += subStep;
subStep=1;
break;
}
}
}
}
}