Ok. So I have an assignment where I have to read in text from a file, take out all the spaces and punctuation, make everything lowercase and then use my "key" to change the letters around so that it can't be read unless you know what the key is. I have accomplished everything up to this point.
The next part is to make another class called Cracker that should read in the ciphertext (or the mixed up part) and try to figure out what the key is based on letter frequency count. I know what i need to do logically to do this, but I am not really good at writing code, so I am struggling on getting it done. So, if anyone could help me, it would be greatly appreciated!
I was trying to do it so that it read in the mixed up text and then used a for loop to count how many times each letter appears and then put them in order from highest frequency to lowest frequency. After it does that, I would then need to compare it to another list of characters. That list holds the following in this order ( e,t,a,o,i,n,s,h,r,d,l,c,u,m,w,f,g,y,p,b,v,k,j,x,q,z). Then it would go through and replace them based on the two lists. So like, if I go through and count and find that B had the highest frequency in the ciphertext, I would then replace it with E because that's the letter that appears most in the english language.
I really hope that made sense....
Joined: Oct 21, 2008
I think you should try to write some code for the Cracker and then we can help with finding the problems. You posted the code for the Cracker. Are you sure you want to cipher a text by changing every 'a' character to 'z'? It will be understandable by humans, but you won't be able to decipher it with the mentioned decipher, since it won't be able to differentiate between 'a' and 'z' characters.
In the cracker algorithm you don't have to do too much. As you mentioned, you count the number of different characters and order them based on that. At this point you have two character list one which contains the characters based on the frequency in real text and the other contains them based on the frequency in the ciphered text. So the only thing you have to do is to recheck the ciphered text character by character and for every character you do the following:
Determine the index of the character in the second list(the one which you made by checking the ciphered text) and then get the character at that index from the first list. That will be the deciphered character.
Joined: Oct 13, 2005
Welcome to JavaRanch
If you do a search through these fora, you will find that people have used Maps (or HashMaps) for counting frequencies.