Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Arithmetic with char types

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

can someone please help me understand the meaning and use of char types in this situation?

Thank you
 
lowercase baba
Posts: 13075
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the method that was given to me for encoding



I'm just looking for someone to explain how this works
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 = ?


Does that help you see how it works?
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks folks, i got it a little clearer now.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic