Created
July 13, 2017 16:05
-
-
Save sujaykundu777/9ee3328bf15fcc2b41a3576cf08b996c to your computer and use it in GitHub Desktop.
Run Length Encoding in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can this be modified to work when one letter is entered to give the output of A1?