-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaesarCipher.java
36 lines (35 loc) · 1.4 KB
/
CaesarCipher.java
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
29
30
31
32
33
34
35
36
// Program to encode/decode using caesar cipher.
public class CaesarCipher {
// lower limit " " - 32, upper limit "~" - 126
static String encode(String oriWord, int noOfShift) {
String cipherText = "";
int letter, extraShift;
for(int i = 0; i < oriWord.length(); i++) {
letter = Character.toLowerCase(oriWord.charAt(i));
if (Character.isLetter(letter)) {
if(letter + noOfShift > 'z') {
// calculating extra shift of the letter over and above 'z'
extraShift = (letter + noOfShift) - ('z' + 1);
// adding extra shift to 'a'
letter = 'a' + extraShift;
} else
letter += noOfShift;
} else {
if (letter + noOfShift > '~') {
// calculating extra shift of the letter over and above '~'
extraShift = (letter + noOfShift) - ('~' + 1);
// adding extra shift to ' ' (whitespace)
letter = ' ' + extraShift;
} else
letter += noOfShift;
}
cipherText += (char) letter;
}
return cipherText;
}
static String decode(String oriWord, int noOfShift) {
String plainText = "";
plainText = encode(oriWord, 26 - noOfShift);
return plainText;
}
}