-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode
More file actions
29 lines (18 loc) · 714 Bytes
/
decode
File metadata and controls
29 lines (18 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Ceasar Cipher
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 ‘e’.
Implement a decoder for the Ceasar cipher where N = 5.
*/
public String decode(String code) {
char[] c = code.toCharArray();
for (int i=0; i<c.length; i++) {
if (c[i]+5 > 'z') {
c[i] = (char)(c[i]+5 - ('z'-'a'+1)); // alphabet length = 'z'-'a'+1
}
else c[i] = (char)(c[i]+5);
}
return String.valueOf(c);
}