# Arithmetic with char types

tom jenkins
Greenhorn
Posts: 27
Hello Folks,

I'm currently working on a project that involves programming a Caesar ciphering program.

Currently I'm supposed to develop a way to wrap around the alphabet to meet the requirements of the encoding method for the Caesar shift.

This was given to me:

char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);

I understand that this arithmetic is cast as a char type, but I'm not sure as how the whole a+ch+3 ..ect.. bit works. I know that char types have numeric values and can be used in arithmetic, but I'm not really so clear as to how this works

Thank you

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
that doesn't make a lot of sense to me.

the 'a' - 'a' cancels itself out. effectively I believe you have this:

ch + 3 % 26

Which shifts each letter three places up, so 'a' becomes 'd', 'b' becomes 'e', etc. But what happens to 'x','y', and 'z'? If you add three to their ascii values, you get '{', '|', and '}'.

You need to somehow roll those back down to the values for 'a', 'b', and 'c'. But using the mod operator won't do that...it would give you some value between 0 and 25, which are NOT the ascii values for the alphabet at ALL.

Who gave you this code? I think they were crazy.

OR

I need more coffee.

or both, i guess.

Winston Gutkowski
Bartender
Posts: 10422
63
tom jenkins wrote:This was given to me:
char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);
I understand that this arithmetic is cast as a char type, but I'm not sure as how the whole a+ch+3 ..ect.. bit works...

Me neither, because
(a) it won't compile.
(b) if it's only supposed to rotate alphabetic letters (and specifically, lower-case ones), it's wrong.

Winston

tom jenkins
Greenhorn
Posts: 27
Well this assignment is from my professor, I guess he's not very clear at times.. Here is some more.. but I guess this was to serve as some sort of example.

Abstract class Cipher

The class Caesar inherits the abstract class Cipher. This class defines the methods code and decode. The method encode takes a String parameter and returns a String result. It takes each character of the parameter and performs a Caesar shift on the character. That is, a shift with possible wrap around can be coded as follows:

char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);

not sure what the modulus is for really.. can anyone point me to a more clearer strategy to take on wrapping around the alphabet and making the encoding method?

Thanks

tom jenkins
Greenhorn
Posts: 27
this is the method that was given to me for encoding

I'm just looking for someone to explain how this works

Matthew Brown
Bartender
Posts: 4567
8
tom jenkins wrote:

Those brackets make a world of difference, because the % has higher precedence than the +.

Roughly speaking, that line translates as
- shift the character so it's a number between 0 and 25 (inclusive)*
- shift it by a fixed amount (the cipher)
- take the remainder when you divide by 26. That makes sure you've still got a value between 0 and 25. The effect of this is for your shift to wrap round.
- Convert it back to a letter.

* assuming it's a lower-case letter. This approach won't work for anything except a-z.

Try it out with some representative values:

ch = 'x'
ch - 'a' = ?
ch - 'a' + 5 = ?
(ch - 'a' + 5) % 26 = ?
'a' + (ch - 'a' + 5) % 26 = ?

Rob Spoor
Sheriff
Posts: 20546
56
ch - 'a' takes the ASCII value of ch and subtracts the ASCII value of 'a'. If ch is a lowercase letter then this will return a value between 0 and 26 (exclusive). The shift is added, then the mod operator turns it back into a value between 0 and 26 (assuming that Constants.WRAP_AROUND is 26). You add that back to 'a' to again get a lowercase letter.

tom jenkins
Greenhorn
Posts: 27
thanks folks, i got it a little clearer now.