I'm doing some practice on HackerRank and I'm stuck on a problem. The problem is:
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc
Example
input:
11
middle-Outz
2
output:
okffng-Qwvb
I pass this test case, but I don't pass the rest of the test cases and it won't tell me which ones. What am I missing in my logic?
public static String caesarCipher(String s, int k) {
String newString = "";
// Write your code here
for (int i=0; i<s.length(); i++) {
char currentChar = s.charAt(i);
int currentAscii = currentChar;
if (currentAscii <65|| currentAscii>122 || (currentAscii>90 && currentAscii < 97)) {
newString += currentChar;
} else {
currentAscii = currentChar+k;
if (currentAscii > 122) {
currentAscii = Math.abs(currentAscii-26);
}
newString += (char) currentAscii;
}
}
return newString;
}
.