Introduction to Programming Machine Problem III from Spring 1993


Caesar cyphers are only one form of simple substitution cypher. More generally, substitution cyphers involve replacing any letter with any other letter. For example, consider the following letter substitution table:


The line below is this line as encrypted using the table above.
YLK edNK JKefQ dx PLdx edNK rx KNbgzwPKs hxdNt PLK PrJeK rJfyK.
To encrypt a letter, find it in the top row of the above table and use the corresponding letter from the bottom row. To decrypt a letter, find it in the bottom row and use the corresponding letter from the top row.


Write a program which performs a general substitution cypher. Your program should read the details of the substitution it is to make from a file called key, but other than that, it should operate in the same way as your solution to machine problem II. That is, it should read text from standard input, make the substitutions dictated by the key file, and write the result to standard output.

The Key file should contain exactly two lines of text in the format shown above. The first line should list the characters that are to be changed, and the second line should list the character to be substituted for each of the changed characters. Any character not listed in the first line should remain unchanged.

The first line of the key file should not list any character more than once, but if it does, the substitution given for the last occurance of the duplicated character should be the one that counts. If you encrypt a file, then change the order of the two lines in the key file, your program will then be set up to decrypt the encrypted file!

The file crypt contains English text that was encrypted with the key file in key. Once you compile your program, you should be able to use the information in key to make your own key for decrypting the contents of crypt.

Grading Criteria

This problem is worth 5% of your 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.