introduction: C PROGRAMMING
For this assignment you will write an encoder and a decoder fora modified \"book cipher.\" A book cipher uses a document or book asthe cipher key, and the cipher itself uses numbers that referencethe words within the text. For example, one of the Beale ciphersused an edition of The Declaration of Independence as the cipherkey. The cipher you will write will use a pair of numberscorresponding to each letter in the text. The first number denotesthe position of a word in the key text (starting at 0), and thesecond number denotes the position of the letter in the word (alsostarting at 0). For instance, given the following key text (thenumbers correspond to the index of the first word in the line)
[0] 'Twas brillig, and the slithy toves Did gyre and gimble inthe wabe;
[13] All mimsy were the borogoves, And the mome rathsoutgrabe.
[23] \"Beware the Jabberwock, my son! The jaws that bite, theclaws that catch!
[36] Beware the Jubjub bird, and shun The frumiousBandersnatch!\"
[45] He took his vorpal sword in hand: Long time the manxome foehe sought—
The word \"computer\" can be encoded with the following pairs ofnumbers:
35,0 catch!
5,1 toves
43,3 frumious
48,3 vorpal
22,1 outgrabe.
34,3 that
23,6 \"Beware
7,2 gyre
Placing these pairs into a cipher text, we get thefollowing:
35,0,5,1,43,3,48,3,22,1,34,3,23,6,7,2
If you are encoding a phrase, rather than just a single word,spaces in the original english phrase will also appear in theciphered text. So, the phrase \"all done\" (using the aboveJabberwocky poem) might appear as: 0,3,1,4,13,16,0,46,2,44,2,3,2
Only spaces in the key text should be considered delimiters. Allother punctuation in the key text are to be considered part of akey word. Thus the first word in the Jabberwocky poem, 'Twas, willhave the following characters and positions for key word 0:
Position 0: '
Position 1: T
Position 2: w
Position 3: a
Position 4: s
Developing the Program:
You should approach this assignment in several parts. The firstpart will be to write a menu driven program that prompts the userfor the following actions:
- Read in the name of a text file to use as a cipher key
- Create a cipher using the input text file (and save the resultto a file)
- Decode an existing cipher (prompt user for a file to readcontaining the cipher text)
- Exit the program
For each choice, create a stub function that will be completedin the remaining steps.
After testing your menu, continue to fill in the stub functionswith the following specifications:
Choice #1
For this menu choice, you will prompt the user for the name of acipher text file (such as the Declaration of Independence). Youwill read this text file line by line, and place each word, inorder, in an array of char strings. As you copy each word into thearray, you will keep a running count of the number of words in thetext file and convert the letters of each word to lower case. Youmay assume that you will use no more than the first 5,000 words inthe text, and no more than the first 15 characters in a word.However, there is no guarantee that the text will have 5000 wordsor less and any word in the text is 15 characters or less.
Choice #2
If no file has been chosen for Choice #1 (i.e. the user goesdirectly to Choice #2 without first choosing Choice #1), you willfirst prompt the user to enter a cipher text and read it intomemory (presumably by calling the function that would be called byChoice #1). You will prompt the user to enter a secret message (inplain text - such as \"Computer Science is the best major at GMU!\")that is terminated by pressing the \"Enter\" key. You can assume thatthe length of this message will be less than 1500 characters(including the carriage return and NULL terminator). You will thenparse this message, character by character, converting them tolower case as you go, and find corresponding characters in thewords found in the key text word array. You can do this by goingthrough each word in the word array, and then each character ineach word, until you find a match with the current messagecharacter. There are more efficient ways to perform this operation,and you are encouraged to implement them instead of the givenmethod. Once a character match is found, you will write the indexof the word and the index of the character to a character stringthat you will later write out to a text file. Spaces are to beplaced into the text as found in the message and will be used todelimit the separate words in the secret message. Once the messagehas been encoded, prompt the user for the name of a file to savethe encoded message to, and save it to that file.
Choice #3
You will prompt the user for the name of a file containing anencoded text (i.e. a file containing number pairs). Your programwill read the file and decode the the text using the indexes pairsgiven for each character in the word and the text file chosen forChoice #1. If no file has been chosen for Choice #1 (i.e. the usersgoes directly to Choice #3 without first choosing Choice #1), youwill prompt the user to enter a cipher text and read it into memory(presumably by calling the function that would be called by Choice#1). Spaces found in the file are to be treated as spaces in thedecoded text. You can assume that the number of characters in theencoded text file is 5000 or less, including any carriage returnsor NULL terminator characters. Once the text is decoded, print themessage to standard output.
Choice #4
Exit the program.
Additional Specifications:
- In order to introduce some \"randomness\" in the specificcharacter encoding, you will generate a random number i from 0..9inclusive (use the last four digits of your G Number as the seed),and use the ith instance of that character found in the text. (Iffewer than i instances of the character is found in the text, loopback and continue the search from the beginning of the document.)
- Example: Suppose the letter to encode is a 'c'. Using thesentences just above, we find that there are the following 'c'characters:
- In order to introduCe some \"randomness\" inthe speCifiCCharaCterenCoding, you will generate a randomnumber i from 0..9 inClusive (use thelast four digits of your G Number as the seed), and use the ithinstanCe of thatCharaCter foundin the text.
- If the random number returns 6, then you will use the 'c' fromthe word \"inclusive.\" (Start counting from 0). If the random numberreturns 2, you would the second c found in the word\"specific.\"
- If a given character in the secret message is not found in anyword of the text, replace that character with the '#' character inthe encoded text (a single '#' character replaces a word/positionpair).
- Files and filenames will follow the standard CS262 conventions(username, lab section, etc. at top of file, and as part offilename).
- Each menu choice (except #4) should call a separate function toperform the operation.
- You can assume that the message to encrypt or decrypt will beless than 1500 characters.
- Your program will be compiled using a Makefile
Predefined C functions that may be useful for thisproject:
strtok()
strlen()
tolower()
atoi()
Options for extra credit:
- Use dynamic memory for the arrays (up to 5 points additionalcredit)
- Finding the original Beale treasure and sharing it with yourprofessor (up to 1000 points additional credit)