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.




declare (c1, c2) character (1);
declare run_length fixed binary;
declare input file;
 
open file (input) title ('/RLE.DAT,type(text),recsize(20000)');
on endfile (input) go to epilog;
 
get file (input) edit (c1) (a(1));
run_length = 1;
do forever;
   get file (input) edit (c2) (a(1));
   if c1 = c2 then
      run_length = run_length + 1;
   else
      do; put edit (trim(run_length), c1) (a); run_length=1; end;
   c1 = c2;
end;
epilog:
   put edit (trim(run_length), c1) (a);
   put skip;
 
 
/* The reverse of the above operation: */
declare c character (1);
declare i fixed binary;
declare new file;
 
open file (new) title ('/NEW.DAT,type(text),recsize(20000)');
on endfile (new) stop;
do forever;
   run_length = 0;
   do forever;
      get file (new) edit (c) (a(1));
      if index('0123456789', c) = 0 then leave;
      run_length = run_length*10 + c;
   end;
   put edit ((c do i = 1 to run_length)) (a);
end;