Untitled

 avatar
unknown
c_cpp
2 months ago
575 B
4
Indexable
#include <stdio.h>
#include <string.h>

void compressString(const char *str) {
    int len = strlen(str);
    
    for (int i = 0; i < len; i++) {
        int count = 1;
        
        // Count consecutive occurrences of the same character
        while (i < len - 1 && str[i] == str[i + 1]) {
            count++;
            i++;
        }

        // Print character followed by count
        printf("%c%d", str[i], count);
    }
    
    printf("\n");
}

int main() {
    char str[] = "aaabbaaccdee";
    compressString(str);
    return 0;
}
Editor is loading...
Leave a Comment