For this assignment you will write an encoder (in C!!) and a decoder for a modif
ID: 3681755 • Letter: F
Question
For this assignment you will write an encoder (in C!!) and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the position of a word in the key text (starting at 0), and the second number denotes the position of the letter in the word (also starting at 0). For instance, given the following key text (the numbers correspond to the index of the first word in the line):
[0] 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe;
[13] All mimsy were the borogoves, And the mome raths outgrabe.
[23] "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
[36] Beware the Jubjub bird, and shun The frumious Bandersnatch!"
[45] He took his vorpal sword in hand: Long time the manxome foe he sought—
The word "computer" can be encoded with the following pairs of numbers:
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 the following: 35,0,5,1,42,3,48,3,22,1,34,3,23,5,7,2
If you are encoding a phrase, rather than just a single word, spaces in the original english phrase will also appear in the ciphered text. So, the phrase "all done" (using the above Jabberwocky poem) might appear as:
0,3,1,4,13,1 6,0,46,2,44,2,3,2
Only spaces in the key text should be considered delimiters. All other punctuation in the key text are to be considered part of a key word. Thus the first word in the Jabberwocky poem,
'Twas, will have the following characters and positions for key word 0:
Position 0: '
Position 1: T
Position 2: w
Position 3: a
Position 4: s
You should approach this assignment in several parts. The first part will be to write a menu driven program that prompts the user for the following actions:
1) Read in the name of a text file to use as a cipher key
2) Create a cipher using the input text file (and save the result to a file)
3) Decode an existing cipher (prompt user for a file to read containing the cipher text)
4) Exit the program For each choice, create a stub function that will be completed in the remaining steps.
After testing your menu, continue to fill in the stub functions with the following specifications:
Choice #1 For this menu choice, you will prompt the user for the name of a cipher text file. You will read this text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. You may assume that you will use no more than the first 5,000 words in the text, and no more than the first 15 characters in a word. However, there is no guarantee that the text will have 5000 words or 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 goes directly to Choice #2 without first choosing Choice #1), you will first 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). You will prompt the user to enter a secret message that is terminated by the "enter" key. You will then parse this message, character by character, converting them to lower case as you go, and find corresponding characters in the words found in the key text word array. You can do this by going through each word in the word array, and then each character in each word, until you find a match with the current message character. There are more efficient ways to perform this operation, and you are encouraged to implement them instead of the given method. Once a character match is found, you will write the index of the word and the index of the character to a character string that you will later write out to a text file. Spaces are to be placed into the text as found in the message and will be used to delimit the separate words in the secret message. Once the message has been encoded, prompt the user for the name of a file to save the encoded message to, and save it to that file.
Choice #3 You will prompt the user for the name of a file containing an encoded text. Your program will read the file and decode the the text using the indexes pairs given for each character in the word and the text file chosen for Choice #1. If no file has been chosen for Choice #1 (i.e. the users goes directly to Choice #3 without first choosing Choice #1), you will 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 the decoded text. Once the text is decoded print the message to standard output.
Choice #4 Exit the program.
Additional Specifications In order to introduce some "randomness" in the specific character encoding, you will generate a random number i from 0..9 inclusive and use the ith instance of that character found in the text. (If fewer than i instances of the character is found in the text, loop back and continue the search from the beginning of the document.) If a given character in the secret message is not found in any word of the text,replace that character with the '#' character in the encoded text (a single '#' character replaces a word/position pair).
Explanation / Answer
import sun.security.util.Length; import java.io.File; import java.io.FileNotFoundException; import java.util.*; /** * Satisfies the requirements for Question #2 * of Fall CS210 final. * * @author Pat Kaehuaea * 12/6/12 * 7:17 PM */ public class Question2 { public static void main(String[] Args) throws FileNotFoundException{ Scanner inputFile = new Scanner(new File("src/question2.txt")); textCount(inputFile); } /** * Method reads a file line by line, in order to determine * the total number of lines, the length of the longest line, * and which line is longest. The method prints these values * to console as output. * * @param input Scanner object holding file handle for input */ public static void textCount(Scanner input){ //initialize place holder values int numLines = 0; int lengthLongestLine = 0; String longestLine = ""; //process line by line while(input.hasNextLine()){ //assert file has at least one line //increment numLines numLines++; //read each line into String String line = input.nextLine(); //determine if this line is greater than lengthLongestLine if(line.length() > longestLine.length()){ //set lengthLongestLine and longestLine lengthLongestLine = line.length(); longestLine = line; } } //print statistics System.out.println("Total lines = " + numLines); System.out.println("Length of longest line = " + lengthLongestLine); System.out.println("Longest line = " + longestLine); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.