If this string compression code is given a string aaaabbbbcc, it will produce the string a4b4c2. However, the solution doesn’t work if the occurrence count of a character is greater than 9. What changes should be made?
void convert(char *s) {
char *p = s;
char *q = s;
int c;
int counter;
while (*p) {
c = *p;
counter = 0;
while (*p && *p == c) {
p++;
counter++;
}
*q++ = c;
*q++ = counter+'0';
}
*q = 0;
}
The issue is: there is no ASCII value for a number larger than 9. So, how to store that number in the string?
since these patterns do not normally occur in human readable form, I will deduce that it only needs to be machine readable, since 99.9% of words will have negative compression even if they have repeating chars
ex.
vaccuum -> v1a1c2u2m1
you don’t need to use int for a counter, you can simply use:
then you can just change
if you do need a human readable output use something like: