Run Length Encoding in C#

If the input string is “qqqhqhhjoooooeq”, then the function should return “q5h3j1o5e1″.

class Run_Length_Encoding
{
static void Main(string[] args)
{
Console.WriteLine(“Please enter a string: “);
string strInput = Console.ReadLine();
Dictionary<char, int=””> dict = new Dictionary<char, int=””>();
bool notCompleted = false;
foreach (char ch in strInput)
{
if (dict.ContainsKey(ch))
{
dict[ch] = dict[ch] + 1;
}
else
{
dict.Add(ch, 1);
}

if (dict.Keys.Count() * 2 > strInput.Length)
{
Console.WriteLine(“Final string is greater then input string and it’s not allowd”);
notCompleted = true;
break;
}
}

if (!notCompleted)
{
strInput = string.Empty;
foreach (KeyValuePair<char, int=””> item in dict)
{
strInput += item.Key.ToString() + item.Value.ToString();
}
Console.WriteLine(string.Format(“Final string is: {0}”, strInput));
}
Console.ReadKey();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s