CS1101C Practical Exam
Question 4 (xxxx - xxxx hours)
Repeated Character Encoding
The name of your C program file must be called decode.c, files with any
other name will not be marked.
The deadline for this lab is Wednesday 12 November 2008, xx:xx:xx
hours. Strictly no submissions will be accepted after the
deadline.
Many encoding systems have been designed to encode strings with many
repeated consecutive characters. Here, we will decode a very simple
encoding system.
Let us look at the following examples. In each of the following, you are
given an alphanumeric string. If it contains any digits or the
full-stop, then you will need to decode it. Display the decoded
string.
- abcdeeefg is decoded as abcdeeefg because there are
only letters in the string.
- abcde3efg is decoded as abcdeeeefg. When you see
digits after a letter (say the digits make up the number n), it
means that there are n occurrences of that letter (also known as
its frequency). For example, e3 decodes to eee. Note that
the frequency can be up to four digits long.
- abcde3e2fg is decoded as abcdeeeeefg because e3
decodes to eee and e2 decodes to ee.
- cb2crf12 is decoded as cbbcrffffffffffff because
b2 decodes to bb and f12 decodes to
ffffffffffff (12 fs).
- cb1crf12hg. is decoded as
cbcrffffffffffffhgggggggggggg because b1 (bee one) decodes
to one b, f12 decodes to 12 fs, and g. decodes to
12 gs (the full-stop means repeat the same number of times as the
previous frequency; there can be more than one full-stop in the string).
- ca3b11e3ikf.gh4 is decoded as
caaabbbbbbbbbbbeeeikfffghhhh because a3 decodes to
aaa, b11 decodes to 11 bs, e3 decodes to
eee, f. decodes to fff (3 fs since the previous
frequency was 3), and finally h4 decodes to hhhh.
Text File
Assume that the text file containing the encoded strings is always
called “format1.txt” and that this file always
exists. (It has been copied into your directory by the
“pesetup” command.) Let us look at the file's
contents:
abcdeeefg
abcde3efg
abcde3e2fg
cb2crf12
cb1crf12hg.
ca3b11e3ikf.gh4
Each line consists of an encoded string that you must decode. You may
assume that the encoded strings consists only of lowercase letters,
digits, and the full-stop. You may also assume that the full-stop will
never come before a digit in the string. The encoded string has at most
60 characters.
Your task is to read each encoded string from the text file and display
the results on the screen as shown:
Processing abcdeeefg:
abcdeeefg
Processing abcde3efg:
abcdeeeefg
Processing abcde3e2fg:
abcdeeeeefg
Processing cb2crf12:
cbbcrffffffffffff
Processing cb1crf12hg.:
cbcrffffffffffffhgggggggggggg
Processing ca3b11e3ikf.gh4:
caaabbbbbbbbbbbeeeikfffghhhh
Note that the number of lines in the text file is not known. You are to
read the file until you reach the end of the text file.
Important Notes
Do not use any structures or any form of dynamic memory allocation
(using malloc or calloc) in your program, else no
credit will be given.
Remember to submit your program frequently using the submit
decode.c command, and check your submission using the check
command.
All the best!
UNIX commands
Some useful UNIX commands (in case you forgot what you did in Lab 0):
- “dir”: lists all the files in the directory.
- “cp a.txt b.txt”: copies a.txt to b.txt.
- “mv a.txt b.txt”: moves / renames a.txt to
b.txt.
- “cat a.txt”: shows the contents of a.txt.