Q:

Implement a decoder for the Caesar cipher where N = 5

belongs to collection: java exercises (medium level )

0

The Ceasar cipher is a basic encryption technique used by Julius Ceasar to securely communicate with his generals. Each letter is replaced by another letter N positions down the english alphabet. For example, for a rotation of 5, the letter c would be replaced by an h. In case of a z, the alphabet rotates and it is transformed into a d.
Implement a decoder for the Caesar cipher where N = 5.
TIP: Use code.toCharArray() to get an array of characters.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public String decode(String code) {
String decoded = "";
int lastCharValue = 'z';
int alphabetLength = 'z' - 'a' + 1;
for (char character: code.toCharArray()) {
    int charNoRotation = character + 5;
    int charValue = charNoRotation < lastCharValue ? charNoRotation : charNoRotation - alphabetLength;
    decoded += (char) charValue;
}
return decoded;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a method that returns a comma-separated stri... >>
<< Write a method that checks if there is at least on...