Introduction to Programming Machine Problem II from Spring 1993

Background

Julius Caesar is supposed to have used secret codes known today as Caesar cyphers. There are 26 such cyphers over the English alphabet. In the simplest, A is replaced with B, B is replaced with C, and so on, up to Z, which is replaced with A. This is called a rotate-one Caesar cypher because it rotates the alphabet one place. A rotate-two cypher replaces A with C, B with D, and so on up to Z which is replaced by B.

The following line is this line encrypted with a rotate one cypher.
Uif gpmmpxjoh mjof jt uijt mjof fodszqufe xjui b spubuf pof dzqifs.
Note that the letters in the above have been changed, but spaces and punctuation marks have not. Also, capitalization has been preserved.

For most Caesar cyphers, decryption and encryption require different algorithms, but not for the rotate-13 cypher; here, the same algorithm will both encrypt and decrypt. This is because there are 26 letters in our alphabet, so doing rot13 twice brings each letter back to itself.

Assignment

Write a non-interactive program that reads a message and writes the rot13 encrypted form as output. This program must replace every letter in the message by the rot13 equivalent, while leaving digits, spaces, punctuation, and anything else unchanged. Your program must include and use the Boolean functions isupper(ch) and islower(ch) to detect upper and lower case letters. Your program must include a procedure, rot13(ch), that takes ch (a character variable), and if ch is a letter, changes it to its rot13 equivalent.

The file rot13 contains English text, in rot13 encrypted form. Once you compile your program, the following command should decrypt this file and list it on your screen:

  % a.out < rot13
To see what your program does when you give it to itself as input, try the following (this puts the result in a file called mp2.rot13):
  % a.out < mp2.p > mp2.rot13
Finally, try the following to decrypt mp2.rot13 and page through it:
  % a.out < mp2.rot13 | more

Grading Criteria

This problem is worth 5% of your final grade. If you get your assignment to work perfectly, you will earn only half of credit. The other half will depend on the style of your code and commentary.