Run-Length Encoding
Completed
Read a word and print its run-length encoding: each run of the same letter becomes the letter followed by the run length (e.g. aaabcc → a3b1c2).
Input
One word.
Output
The encoding.
Example
Input
aaabcc
Output
a3b1c2
Editor
C
Hints
The inner loop counts a run; the outer loop moves from run to run.
Solution explanation
Run-length encoding is a simple compression scheme used in early image formats.
Show solution
#include <stdio.h>
int main() {
char s[200];
scanf("%s", s);
int i = 0;
while (s[i] != '\0') {
char c = s[i];
int run = 0;
while (s[i] == c) {
run++;
i++;
}
printf("%c%d", c, run);
}
printf("\n");
return 0;
}