Department of Computer Science

Principles of Functional Languages

Fall Semester 2010

Dr. Maury Eggen

Homework Laboratory Assignment 5:

Homework assignment five involves a simple substitution encoding of a body of text. We shall read a body of text into a string, and then encode the string. The encoding works as follows:

First the given string is converted all to lower case, then compressed to eliminate all but the alphabetic characters. (You may want to save spaces as well to make the text easier to read, but that's optional)

A code word is then given. Say, for example the code word is "code." (It could be any suitable code word). Based on this code word, substitution alphabets are generated. For the code word "code" the alphabets would be:

cdefghijklmnopqrstuvwxyzab
opqrstuvwxyzabcdefghijklmn
defghijklmnopqrstuvwxyzabc
efghijklmnopqrstuvwxyzabcd

Assume the original text is:
Dear mom: School is great. Send money. Love, your son
then the compressed lower case text is:
dearmomschoolisgreatsendmoneyloveyourson

We look up the characters of the original text in the parent alphabet
abcdefghijklmnopqrstuvwxyz
and select replacement characters from the code alphabets indicated above. The encoding for the given text would be:
d -- f (d is 3rd character of alpha, f is third character of first encoding alphabet)
e -- s (e is 4th character of alpha, s is fourth character of second encoding alphabet)
a -- d (a is 0th char of alpha, d is 0th char of third encoding alphabet)
r -- v (17th char)
and so on. We now go back to the first of the encoding alphabets for our next character.

Your program should be able to encode any text based on any code word. You may assume the code word is all alphabetic -- simplifies things a bit. More credit can be given for more general coding.

Then, there is general problem of decoding. Given the encoded text, the decoder must be able to reproduce the original text. Well, not all of the original text, but only the part that was actually encoded. If you received a message that read: dearmomschoolisgreatsendmoneyloveyourson could you read it? I think so. If you saved spaces, then of course the original would be easier to read. You would simply include space in the alphabet, and also in the encoding alphabets and have a 27 character alphabet instead of a 26 character alphabet.

Ultimately, I would like to be able to (encode text) where text is a text string. I would also like to (decode encodedtext) producing the original. You may write separate functions which read files into strings and write strings to files.

Homework due date: Friday, 22 Oct 2010