• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple logic error in cipher-shifting program?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! Thanks for clicking on this post. I could use a fresh pair of eyes.
I was tasked with creating a program that encrypts a line of text (for example, CANDY) by shifting the letters X amount of times. For example, if the user inputs the sentence CANDY and selects a shift of 5, the output would be:
HFSID
I got this part working fine. The issue I am having is with the decryption part of the program. This is simply the reverse of the above, as the user would enter the phrase HFSID, with a shift of 5, and the program would output:
CANDY
It works fine, all except for one letter, being the "F" letter. With my code, when I enter the above word to be decrypted it outputs:
C[NDY
Obviously, that [ bracket is not an 'A'. I realise the issue falls with the equation, but upon messing around with the program for about four hours, it is all starting to blur together. I just need a point in the right direction...
Here is my code:


Any help is appreciated!
 
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 34: I think you meant 26 not 25.
Line 35: Less than zero.
Line 36: rethink this formula.
I would also correct for negative values before doing the modulus.
 
Saloon Keeper
Posts: 15561
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't your decryption a simple reversal of your encryption?

You can reverse the modulus by simply adding 26 once and then applying the modulus.

( 5 + 26) % 26 = 5
(-2 + 26) % 26 = 24

 
reply
    Bookmark Topic Watch Topic
  • New Topic