Feb 28, 2009

run_len_encoding

char* run_len_encoding(char* str, char* newStr)//str is composed by 1s and 0s
{
if(!str||!newStr) return NULL;

char curr[2];
char prev[2];
curr[1]='/0'
prev[1]='/0'
prev[0]=*str;
int count=1;

do{
str++;
curr[0]=*str;
if(prev[0]==curr[0])
count++;
else
{
strcat(newStr,const_cast&prev);
strcat(newStr,const_castitoa(count));
count=1;
prev[0]=curr[0];
}
}while(*str!='/0')

return newStr;
}

No comments:

Post a Comment