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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply