Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

Due to writing limit, I have posted the code here: http://pastebin.com/cSGcxXGp Hi guys, I'm trying to go trace some code to see that I understand what's happening as we are asked to trace code in the exam. I was looking through this encrypting program that apparently used cesars' cipher but I can only understand how it works to where my last comment in the program is i.e "char c = s.charAt(i)". Any feedback would be appreciated.

OpenStudy (anonymous):

I'm not too familiar with Java, but I'll try my best to help. Is the code working 100% correctly?

OpenStudy (anonymous):

Not exactly like it should. It needs to shift 3 letters. For e.g, meet should be encrypted as phhw but it only shifts once so it is encrypted as ldds.

OpenStudy (anonymous):

But it does compile and run. Sorry if it seems confusing :/

OpenStudy (anonymous):

I'll just run through what it's currently doing: after char c = s.charAt(i); (which I assume gets the character at index i of the input string): 3 things happen within the loop: 1. The character's ASCII character code is stored as charCode: (int charCode = (int) c;)

OpenStudy (anonymous):

2. after that, the charactercode is decremented once, corresponding to a shift of one letter by the cipher.

OpenStudy (anonymous):

3. It appends that encrypted character to the output string

OpenStudy (anonymous):

Ohh ok I'm pretty sure I get it now. I'll see if I can mess around and get it shifting by 3 :) Thanks so much!!

OpenStudy (anonymous):

Yeah, but be sure to test it against the entire alphabet, both upper and lowercase.

OpenStudy (anonymous):

because if you shift A (65 in ascii) 1 unit down, it becomes @ (64 in ascii), which is not intended :(

OpenStudy (anonymous):

Will do. I'll use the asciitable site to see if I can work it out then. Thanks again :)

OpenStudy (anonymous):

I've found a decent example of a Caeser Cipher implemented in Java: http://rosettacode.org/wiki/Caesar_cipher#Java

OpenStudy (anonymous):

Caesar* :-P

OpenStudy (anonymous):

Lol I knew there was an "ae" somewhere :P I'll look at both codes and see if I can understand them.

OpenStudy (anonymous):

for (int i=0; i < s.length(); i++) { char c = s.charAt(i); // This returns the character at the specified index in a string int charCode = (int) c; charCode = charCode + 3; // This converts a string to HTML Unicode if ( c == ' ' ) { charCode = 32; } else if ( c == 'x' ) { charCode = 65; } else if (c == 'y' ) { charCode = 66; } else if ( c == 'z' ) { charCode = 67; } output = output + (char)charCode; } System.out.println("The encrypted message is: " + output); // This prints out the encrypted output } } I tested the program with the quick brown fox jumps over the lazy dog. But x y z return symbols instead of a b c. I looked up the ascii code for a b c and used them in the if statements but the program seems to only execute the first if statement?

OpenStudy (anonymous):

Just to make it easier, 32 is a space 65 is a 66 is b and 67 is c

OpenStudy (anonymous):

charCode = charCode + 3 how does this convert it to utf-8?

OpenStudy (anonymous):

what is the whole if-elif block trying to accomplish?

OpenStudy (anonymous):

you need to shift only by 1 letter?

OpenStudy (anonymous):

Well, if I input THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG I get an output of WKH TXLFN EURZQ IR[ MXPSV RYHU WKH OD]\ GRJ As you can see, where x y and z should be replaced with a b and c, it is instead replaced with [ ] / The first if statement works, so that if there is a space between a word, it outputs a space instead of # which is what was in place of spaces before I put the if statement in.

OpenStudy (anonymous):

I need to shift by 3 letters.

OpenStudy (anonymous):

try using the modulo operator somewhere in your code, as it was done in those links I've shown you.

OpenStudy (anonymous):

charCode = (charCode + 3) % (int)('a' + 26)

OpenStudy (anonymous):

you should use modulo 26 because there are only 26 letters and if Z+3 it wont be C it wil be ] we also just studied this encryption :D

OpenStudy (anonymous):

but you should also remember that you may have capital and non capital letter hElLo woRLd so you need seperate conversion for upper case and lower case

OpenStudy (anonymous):

Or you could simply normalize the characters in the input string (all to uppercase) and then work on those without having to worry about lowercase characters.

OpenStudy (anonymous):

and delete non-alphabetic characters

OpenStudy (anonymous):

Actually, I think I got it. The code worked if I used all lowercase letters in the input. It was only once I entered all uppercase letters that

OpenStudy (anonymous):

I got the symbols so now I just did this if you're interested: if ( c == ' ' ) { charCode = 32; } else if ( c == 'x' ) { charCode = 97; } else if (c == 'y' ) { charCode = 98; } else if ( c == 'z' ) { charCode = 99; } else if ( c == 'X' ) { charCode = 65; } else if (c == 'Y' ) { charCode = 66; } else if ( c == 'Z' ) { charCode = 67; } And it works fine now. But thanks to you both for all your help! :)

OpenStudy (anonymous):

it works but it's actually crappy, if in exam they will ask you to write this you won't be able because you won't know ASCII numbers

OpenStudy (anonymous):

Good point. I'm in college tomorrow so I'll ask my lab tutor if there is an alternative way that I can understand.

OpenStudy (anonymous):

There's actually no need to use the raw numbers, you can just type in the actual characters enclosed in single quotes and then cast them to int

OpenStudy (anonymous):

like for that last one: else if ( c == 'Z' ) { charCode = (int) 'C'; }

OpenStudy (anonymous):

but as Tomas.A said, there's a smarter, easier way to implement a Caesar Cipher than this :-P

OpenStudy (anonymous):

you should use modular division then you can write encoder which can be shifter by any number

OpenStudy (anonymous):

right. study that implementation in the http://rosettacode.org/wiki/Caesar_cipher#Java and you might find ideas.

OpenStudy (anonymous):

Wow that's pretty handy. Thanks agdgdgdgwngo. And Tomas, I'll look at the link agdgdgdgwngo gave me to see if I can work it out. Thanks again guys, I really appreciate it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!