I implemented basic string compression algorithm that uses the counts of repeated characters. For example: the string aabcccccaaa would become a2b1c5a3. What do you think about this, is there a better way to do this?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* compressString(char* arr, int size);
int main() {
char arr[] = "aabcccccaa";
char *str;
printf("Before compression: %s\n", arr);
str = compressString(arr, strlen(arr));
printf("After compression: %s\n", str);
// free allocated memory
free(str);
return 0;
}
char* compressString(char* str, int len) {
char last = str[0];
char *buf = (char *)malloc(len * sizeof(char));
int count = 1;
int j = 0;
for (int i = 1; i < len; i++) {
if (last == str[i]) {
count++;
} else {
buf[j++] = last;
buf[j++] += count + '0';
last = str[i];
count = 1;
}
}
buf[j++] = last;
buf[j] += count + '0';
buf[j] = '\0';
return buf;
}
count
(why single character/digit?), look into PackBits to see how to avoid encoding a length "for every source character change". – greybeard Jan 8 at 5:38