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.




 
STRING PROC FNStringGetDecodeStringCharacterEqualCountS( STRING inS )
 STRING s1[255] = ""
 STRING s2[255] = ""
 STRING s3[255] = ""
 STRING s4[255] = ""
 INTEGER I = 0
 INTEGER J = 0
 INTEGER K = 0
 INTEGER L = 0
 K = Length( inS )
 I = 1 - 1
 REPEAT
  J = 1 - 1
  s3 = ""
  REPEAT
   I = I + 1
   J = J + 1
   s1 = SubStr( inS, I, 1 )
   s3 = s3 + s1
   s4 = SubStr( inS, I + 1, 1 )
  UNTIL ( NOT ( s4 IN '0'..'9' ) )
  FOR L = 1 TO Val( s3 )
   s2 = s2 + s4
  ENDFOR
  I = I + 1
 UNTIL ( I >= ( K - 1 ) )
 RETURN( s2 )
END
//
STRING PROC FNStringGetEncodeStringCharacterEqualCountS( STRING inS )
 STRING s1[255] = ""
 STRING s2[255] = ""
 INTEGER I = 0
 INTEGER J = 0
 INTEGER K = 0
 K = Length( inS )
 I = 1 - 1
 REPEAT
  J = 1 - 1
  REPEAT
   I = I + 1
   J = J + 1
   s1 = SubStr( inS, I, 1 )
  UNTIL ( NOT ( SubStr( inS, I + 1, 1 ) == s1 ) )
  s2 = s2 + Str( J ) + s1
 UNTIL ( I >= ( K - 1 ) )
 RETURN( s2 )
END
//
STRING PROC FNStringGetEncodeDecodeStringCharacterEqualCountS( STRING inS )
 STRING s1[255] = FNStringGetEncodeStringCharacterEqualCountS( inS )
 STRING s2[255] = FNStringGetDecodeStringCharacterEqualCountS( s1 )
 RETURN( s2 )
END
//
PROC Main()
 STRING s1[255] = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
 STRING s2[255] = ""
 IF ( NOT ( Ask( "string: get: encode: decode: string: character: equal: count: inS  = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF
 s2 = FNStringGetEncodeDecodeStringCharacterEqualCountS( s1 )
 Warn( "equal strings if result is 1", ",", " ", "and the result is", ":", " ", s1 == s2 )
END
 