CS1101C Practical Exam

Session 4 (1400 - 1545 hours)

ASCII Art

The name of your C program file must be called ascii.c, files with any other name will not be marked.

Nemo has received some nice pictures from his friend Dory. To keep those pictures safe from prying eyes, Dory has encoded the pictures using a very simple scheme.

Pictures can be drawn using plain ASCII characters. Such pictures are called ASCII art. They make use of the printable characters with ASCII values 32 to 126 inclusive.

In Dory's encoding scheme, the printable ASCII characters form a circular "tape" with values 32, 33, 34, ..., 124, 125, 126, then back to 32 again. Each character is shifted to the right by a certain amount x. Let x = 23. So, if the original character had ASCII value 32 (which represents a space character ' '), shifting it to the right by 23 values gives 55 (which represents '7'). If the original character is 105 (which represents 'i'), shifting it to the right by 23 values gives 33 (which represents '!').

Dory sends each picture to Nemo in a file and tells him the value of x. Your job is to help Nemo to see the original picture by writing a decoding program for him. As Dory knows you are an expert programmer, she also wants you to write an encoding program for her so that she can encode the pictures quickly.

You have decided to write one program that does both encoding and decoding. In addition, your program can encode and decode more than one file. The list of files to encode or decode is always stored in files.txt. A sample files.txt is given below:

43e.txt
77d.txt

The file files.txt may contain multiple lines, each line containing one filename. All information necessary to encode or decode is given in the filename. The above means that we want to process two files, and the operations are given as follows:

  1. 43e.txt: the 'e' means encode the file 43e.txt with value x = 43. The name of the output file must be called 43encoded.txt.
  2. 77d.txt: the 'd' means decode the file 77d.txt with value x = 77. The name of the output file must be called 77decoded.txt.

Your program must work with all values of x from 0 to 94 inclusive for both encoding and decoding, and the output file names must follow accordingly with the input file names as shown above.

Assume all input files exist. Overwrite the output file if it already exists.

Sample input files have been placed in your directory by the pesetup program.

We will test your program with other files.txt files.

All the best!

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. "ls -l": lists all the files in the directory.
  2. "cp a.txt b.txt": copies a.txt to b.txt.
  3. "mv a.txt b.txt": moves / renames a.txt to b.txt.
  4. "cat a.txt": displays the text file a.txt.