Console.WriteLine("Enter the string you would want to compress using RLE format");
string UserInput = Console.ReadLine();
int repitions = 1;
for (int i = 0; i < UserInput.Length; i++)
{
if (UserInput[i] == UserInput[i + 1])
{
repitions++;
}
else
{
Console.WriteLine("{0}{1}", repitions, UserInput[i]);
repitions = 1;
}
}
In line 5, there is an error where it says
index was outside the bounds.
My aim is to use this compression and output the number of times the characters repeats and the type of character then move onto the next. Im not sure what is wrong here.
Im expecting a proper working RLE algorithm.
The immediate reason of the exception is in
comparions: on last character of the
UserInput,i == UserInput.Length - 1and we have index out of range atUserInput[i + 1]. We have to add an extra check for the last character:A more maintable approach is to extract method for
RLE:And then use it: