Run length encoding in C++
#include<conio.h>
#include<iostream.h>
#include<string.h>
void main()
{
clrscr();
char str[10];
cout<<"\n Enter the word: ";
cin>>str;
cout<<"\n After Run length encoding: \n";
int count=1;
for(int i=0;i<strlen(str);i++)
{
if(str[i]==str[i+1])
{
count+=1;
}
else
{
if(count==1)
{
cout<<str[i];
}
else
{
cout<<str[i]<<count;
count=1;
}
}
}
getch();
}
#include<iostream.h>
#include<string.h>
void main()
{
clrscr();
char str[10];
cout<<"\n Enter the word: ";
cin>>str;
cout<<"\n After Run length encoding: \n";
int count=1;
for(int i=0;i<strlen(str);i++)
{
if(str[i]==str[i+1])
{
count+=1;
}
else
{
if(count==1)
{
cout<<str[i];
}
else
{
cout<<str[i]<<count;
count=1;
}
}
}
getch();
}
Comments
Post a Comment