Page 1 of 1

#include #include #include #include #include int getrepeat(char *str,

Posted: Fri Apr 29, 2022 6:58 am
by answerhappygod
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int getrepeat(char *str,char ch)
{
char *ptr=str;
int repeat=0;
while(*ptr==ch)
{
ptr+=1;
repeat++;
}
return repeat;
}
char *runlengthencoding(char *str,char *rle
)
{
char *ptr=str;
char *ptrend=&str[strlen(str)];
char encoding[100];
int repeat;
strcpy(rle,"");
while(ptr<ptrend)
{
repeat=getrepeat(ptr,*ptr);
if(repeat>1)
{
sprintf(encoding,"%c%d",*ptr,repeat);
strcat(rle,encoding);
ptr=ptr+repeat;
}else
{
sprintf(encoding,"%c",*ptr);
strcat(rle,encoding);
ptr++;
}
}
return rle;
}
int getcount(char *rle)
{
int count=0;
//
// Add your code here to return count for the current char
//
return count;
}
char *decode_rle(char *rle, char *str)
{
char *ptr=rle;
char *ptrend=&rle[strlen(rle)];
char decoding[100];
int i=0,j=0,count=0, digit=0;
strcpy(str, "");
//
//
// Add your code here to decode rle
//
//
str = '\0';
return str;
}
int main()
{
char str_org[]="WWWWWWWWWWWWWWWWWWWWWWWWWWWWbAAAABBCBBCBBCaaa";
char rle[100];
runlengthencoding(str_org,rle);
printf("str_org:[%s] => rle:[%s]\n",str_org,rle);
char str_dec[100];
decode_rle(rle,str_dec);
printf("rle:[%s] => str_dec:[%s]\n",rle, str_dec);
return 0;
}
Implement a decoder of RLE (Run Length Encoder) by C, will give
like if the problem solved, please do not modify the code, just
follow the comments