Ok i got it correct. Had to implement int helper = i;
due to i
being lost in 2nd for
loop, in else
statement.
Answer:
static string Compression(string test)
{
string result = string.Empty;
for (int i = 0; i < test.Length; i++)
{
int helper = i;
int counter = 1;
for (int j = i+1; j < test.Length; j++)
{
if (test[i] == test[j])
{
counter++;
if(j == test.Length - 1)
{
return result += test[helper] + counter.ToString();
}
}
else
{
i = j-1;
break;
}
}
result += test[helper] + counter.ToString();
}
return result;
}
solved C# string compression