Run Length Encoding

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

int getcount(char str[],int i,int j)

{ int count=1;

while(str[i]==str[j])

{

count++;

j++;

}

return count;

}

void display(char str[],int i,int c)

{

if(c>3)

{

cout<<“!”<<c<<” “<<str[i];

}

else

{

for(int j=0;j<c;j++)

{

cout<<“\t “<<str[i];

}

}

}

void main()

{

clrscr();

char str[30];

int m;

cout<<“enter string”;

gets(str);

fflush(stdin);

m=strlen(str);

int i=0;

int c,j;

while(i<m)

{

j=i+1;

if(str[i]==str[j])

{

c=getcount(str,i,j);

display(str,i,c);

i=i+c;

}

else

{

cout<<“\t”<<str[i];

i++;

}

}

getch();

}

 

Theory:

http://netghost.narod.ru/gff/graphics/book/ch09_03.htm

 

Leave a comment

Calendar

May 2013
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives