Skip to content

Instantly share code, notes, and snippets.

@sujaykundu777
Created July 13, 2017 16:05
Show Gist options
  • Save sujaykundu777/9ee3328bf15fcc2b41a3576cf08b996c to your computer and use it in GitHub Desktop.
Save sujaykundu777/9ee3328bf15fcc2b41a3576cf08b996c to your computer and use it in GitHub Desktop.
Run Length Encoding in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Write a C# program to implement the below compress the given string using Run Length Algorithm
namespace RunLengthEncoder
{
class Program
{
static void Main(string[] args)
{
//Declare the variables
String S1;
String S2="";
int i;
int c=1;
Console.WriteLine("Run Length Encoder");
Console.WriteLine("Enter a String");
S1 = Console.ReadLine();
Console.WriteLine("You Entered : " + S1);
S1 = S1 + " ";
//Console.ReadLine();
//Compress Using Run Length Encoding
for (i=0;i<S1.Length-1;i++)
{
if (S1[i]==S1[i + 1])
{ //When same increment counter
c++;
}
else {
S2 += S1[i];
S2 += c;
c = 1;
}
}
Console.WriteLine("Output" + "\n" + S2);
Console.ReadLine();
}
}
}
@danielwowkula
Copy link

How can this be modified to work when one letter is entered to give the output of A1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment