Description

In the very early days of computing, images were produced using ASCII characters. Such ASCII Art was very popular and, since storage was limited, simple techniques were developed to compress such images. One such simple technique of lossless compression was Run Length Encoding. This scans an input image for runs (sequences) of 2 or more identical values, and replaces them with two such characters followed by an INT string representing a count of the number of characters, and terminated by a * e.g.

A Line from a piece of ASCII Art image as follows:

,,,,,,]F                                                8,

which is composed of 6 commas, a closed square bracket, a capital F, 48 spaces an eight and a comma, would be represented as:

,,6*]F 48*8,

which is two commas, the number six, a star, a closed square bracket, a capital F, two spaces, the number forty-eight, a star, an eight and a comma.

Write a program to accept as input either the letter "C" or the letter "E" followed by a carriage return. Each subsequent line of input data until EOF contains image data.

If the initial input was "C" this represents "compress", and the lines of input text represent an ASCII Art image. Process this input to compress it using the rules specified above, and then output the compressed version.

If the initial input was "E" this represents "expand", and the lines of input text represent a compressed ASCII Art image. Process this input and expand it using the rules referred to above. Print out this now uncompressed ASCII Art image.

You can test your program by compressing an ASCII Art image, saving the output to a file, editing the file to add E as the first line, then feeding this back into your program to effectively reproduce the original ASCII Art image. If your program is working correctly then the original image and your compressed then decompressed version should be identical.

Input

Either an ASCII art file with a C on the first line (this will be compressed using RLE)
or
a compressed ASCII art file with an E on the first line (this will be expanded).

Output

After compressing an input file, all runs of two or more of any character should be replaced by two of the character, an integer count and a star.

After expanding an input file, all previously compressed sequences should be replaced by the original character runs.

Sample Input

C
#####
####### #**#!!###
#**#!!!!## #****#!!!!#
#****###!!!# #*****#!!!!#
#*******#!!!# #******#!!!!#
#*********#!###!*!*!*#!!!!!# --
#!*!*!*!*!*!#!##########!!!!# /_
###########!##!!!!!!!!!!#!!!# //__
###!!!!!!!!!!!#!!!!!!!!!!!!!#!!!####///
##!#!!!!!!!!!!!#!!!!!!!!!!!!!!!#!!!!!!!#
_ ##!!#!!!!!!!!!!!#!!!!!!!!!!!######!!!!!!!*#
\ ##!!#!!!!!!!!!!!!#!!!!####### #!!!!!!***#
___\#!!!###################***** #...!!*****#
/ #!!!.# ***** # *** #....*******#
#*....# *** # #.......*****#
#**.....## ***** ##........!!****#
#!........## *******#########......#...!!!!!*#
#!...........#######.*****...............#.#..!!!!**#
#*.....##.............#..#...............#...#.!!****#
#*....#.#............#....#............##......!*****#
#*.......##.......###......###........#.......!!!****#
#*.........#######......!!....########.......!!!!!***#
#!!!.................!!!!!!!!.............!!!*******#
#!!!!............!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
#*******!!!!!!!!!!!!!!!!!!!!!!!!!!!!***!!!!*****#
#******!!!!!!!!!!!!!!!!!!!!!!!!!********!!****#
##*****!!!!!!!!!!!!!!!!!!!!!#*************###
##****!!!!!!!!!!!!!!!!!!!!!###******####
####!!!!!!!!!!!!!!!!!!!!!!!!######!#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***##
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*******#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*****#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*##
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!####!!!!!!****##
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!###****##!!!!******##
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##**!!*****#!!!********#
#!!!!!!!!#!!!!!!!!!!!#!!!!!!!#***!!!!***!!!!**********#
#!!!!!!!!!!#!!!!!!!!!#!!!!!!!#****!!!!!*!!!!!!!!!!*****#
#!!!!!!!!!!!#!!!!!!!#!!!!!!!#*!!***!!!!!!!!!!!!!!!!!***#
#!!!!!!!!!!!!#!!!!!!#!!!!!!#*!!!!!*!!!!!!!!!!!!!!!*****#
#!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********#
#!!!!!!!!!!!!!#!!!!!!#!!!!!#****!!!!!!!!!!!!!!**********#
#!!!!!!!!!!!!!#!!!!!!#!!!!!#*****!!!!!!!!!!!!!!*********#
#!!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********##
##!!!!!!!!!!!!!#!!!!!!#!!!!!#*!!!!!!!!!!!!!!!!!!!!!*****#!*##
#!#!!!!!!!!######!!!!!!#!!!!!#**!!!!!!!!!!#########!!!!*#!!**##
#!#!#!!!!!!#!!!!!!!!!!!!#!!!!!#***!!!!!####******!!!#######!!**#
#!#!!##!!!!#!!!!!!!!!!!############*!!!#********!!!!!!!!!!!!!!!**#
#!#!!#!#!!#!!!#!!!!!!!#!!!!!!!!!!!!!!!#***********!!!!!!!!!!!!!!!#
#!#!!!#!#!#!!#!!!!!!!#!!!!#!!!!#!!!!!#**********!!!!!!!!!!!!!!!**#
#!!#!!!#!##!!#!!!!!!#!!!!#!!!!#!!!!!!#************!!!!!!!!!!****#
######### ##########!!!!#!!!!#!!!!!!#**********!!!!!!!!!!!!***#
#################************!!!!!!!!!!**#
#**********!!!!!#########
###############

Sample Output

32*##5*
18*##7* 6*#**2*#!!2*##3*
17*#**2*#!!4*##2* 3*#**4*#!!4*#
16*#**4*##3*!!3*# 2*#**5*#!!4*#
16*#**7*#!!3*# #**6*#!!4*#
16*#**9*#!##3*!*!*!*#!!5*# 8*--2*
16*#!*!*!*!*!*!#!##10*!!4*# 6*/_
16*##11*!##2*!!10*#!!3*# 5*//2*__2*
13*##3*!!11*#!!13*#!!3*##4*//3* 2*\
3*\ 7*##2*!#!!11*#!!15*#!!7*#
3*_\ 4*##2*!!2*#!!11*#!!11*##6*!!7**#
4*\\2* 2*##2*!!2*#!!12*#!!4*##7* 5*#!!6***3*#
2*__3*\\2*#!!3*##19***5* 7*#..3*!!2***5*#
/ 3*\#!!3*.# 7***5* # 5***3* 8*#..4***7*#
5*#*..4*# 8***3* 3*# 14*#..7***5*#
4*#**2*..5*##2* 10***5* 10*##2*..8*!!2***4*#
4*#!..8*##2* 7***7*##9*..6*#..3*!!5**#
3*#!..11*##7*.**5*..15*#.#..2*!!4***2*#
2*#*..5*##2*..13*#..2*#..15*#..3*#.!!2***4*#
2*#*..4*#.#..12*#..4*#..12*##2*..6*!**5*#
2*#*..7*##2*..7*##3*..6*##3*..8*#..7*!!3***4*#
2*#*..9*##7*..6*!!2*..4*##8*..7*!!5***3*#
3*#!!3*..17*!!8*..13*!!3***7*#
4*#!!4*..12*!!27***6*#
5*#**7*!!28***3*!!4***5*#
6*#**6*!!25***8*!!2***4*#
7*##2***5*!!21*#**13*##3*
9*##2***4*!!21*##3***6*##4*
11*##4*!!24*##6*!#
15*#!!30**#
15*#!!29***3*##2*
14*#!!29***6*#
13*#!!29***7*#
12*#!!32***5*#
11*#!!35***2*#
10*#!!39*##2*
9*#!!41**##2*
8*#!!43***3*#
8*#!!33*##4*!!6***4*##2*
7*#!!31*##3***4*##2*!!4***6*##2*
7*#!!29*##2***2*!!2***5*#!!3***8*#
7*#!!8*#!!11*#!!7*#**3*!!4***3*!!4***10*#
6*#!!10*#!!9*#!!7*#**4*!!5**!!10***5*#
6*#!!11*#!!7*#!!7*#*!!2***3*!!17***3*#
6*#!!12*#!!6*#!!6*#*!!5**!!15***5*#
6*#!!12*#!!6*#!!5*#**3*!!17***8*#
5*#!!13*#!!6*#!!5*#**4*!!14***10*#
5*#!!13*#!!6*#!!5*#**5*!!14***9*#
5*#!!13*#!!6*#!!5*#**3*!!17***8*##2*
4*##2*!!13*#!!6*#!!5*#*!!21***5*#!*##2*
3*#!#!!8*##6*!!6*#!!5*#**2*!!10*##9*!!4**#!!2***2*##2*
2*#!#!#!!6*#!!12*#!!5*#**3*!!5*##4***6*!!3*##7*!!2***2*#
#!#!!2*##2*!!4*#!!11*##12**!!3*#**8*!!15***2*#
#!#!!2*#!#!!2*#!!3*#!!7*#!!15*#**11*!!15*#
#!#!!3*#!#!#!!2*#!!7*#!!4*#!!4*#!!5*#**10*!!15***2*#
#!!2*#!!3*#!##2*!!2*#!!6*#!!4*#!!4*#!!6*#**12*!!10***4*#
2*##9* ##10*!!4*#!!4*#!!6*#**10*!!12***3*#
22*##17***12*!!10***2*#
38*#**10*!!5*##9*
39*##15*

HINT

You'll need to use a mono-spaced font to view the art correctly (proportionally spaced fonts distort the layouts),

Use scanf to retrieve individual characters from the the standard input and process them as you read them in (no need to store them in arrays).

You can convert a digit char to an int value (i.e. '1' => 1, '2' => 2 etc) using:

intValue = ((int) inputChar) - 48;
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.