Run-Length Encoding

Lab C Advanced 30 points

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
Sample input available Sample output available
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;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD