Run-length encoding

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression.

The output can be anything, as long as you can recreate the input with it.



Example

 Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
 Output: 12W1B12W3B24W1B14W


Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.




include c:\cxpl\codes;  \intrinsic 'code' declarations
string 0;               \use zero-terminated strings, instead of MSb terminated
 
proc Compress(S);       \Compress string using run-length encoding, & display it
char S;
int  I, C0, C, N;
[I:= 0;
C0:= S(I);  I:= I+1;
repeat  ChOut(0, C0);
        N:= 0;
        repeat  C:= S(I);  I:= I+1;
                N:= N+1;
        until   C#C0;
        if N>1 then IntOut(0, N-1);
        C0:= C;
until   C=0;
]; \Compress
 
proc Expand(S);         \Expand compressed string, and display it
char S;
int  I, C0, C, N;
[I:= 0;
C0:= S(I);  I:= I+1;
repeat  ChOut(0, C0);
        C:= S(I);  I:= I+1;
        if C>=^1 & C<=^9 then
                [N:= 0;
                while C>=^0 & C<=^9 do
                        [N:= N*10 + C-^0;
                        C:= S(I);  I:= I+1;
                        ];
                while N do [ChOut(0, C0);  N:= N-1];
                ];
        C0:= C;
until   C=0;
]; \Expand
 
[Compress("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW");
CrLf(0);
Expand("W11BW11B2W23BW13");  CrLf(0);
]