Lab
Text Reading

"Cipher"
A coded message has been prepared using a standard, constant key replacement technique. For every letter in the original, uncoded message a different letter is chosen and the replacement letter is substituted in every position the original letter appears. For example, the message COMPUTER SCIENCE may be encoded as MQWLDXZG YMUZKMZ.

A solution to the code makes use of a feature of written English called "letter frequency." The frequency with which certain letters appear is relatively constant, with the letter 'E' appearing most frequently and the letter 'Z' appearing least frequently. The frequency chart is given below:

    E T A O N R I S H D L F C M U G Y P W B V K X J Q Z

  1. Read in the text (you may obtain a copy of "cipher.txt" from your teacher) counting the number of times each letter occurs. Place this data into an array of struct. You should be able to accomplish this task on one pass through the file. Show that you have succeeded by printing out the letter alongside the frequency.

  2. Sort the data by frequencies in descending order and print out the letters with their associated frequencies.

  3. The sequence above is only accurate for a large text. The actual frequency chart for this coded message ("cipher.txt") is stored in "replace.txt" as below:

        E T A H O S I N R D B L M W G U C Y F J K V P X Q Z

    Modify your struct, by adding a field to your struct so that each of the read-in letters would be associated with the above frequency list. As you read in the file for the second time, make the substitutions and print out the uncoded message to a file.

    What modifications would you need to make to your lab to enable it to encrypt a message as well as decrypt?


    Continue to:  Unit 3  / Prev  / Next