-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
36 lines (30 loc) · 1004 Bytes
/
Copy pathCaesarCipher.java
File metadata and controls
36 lines (30 loc) · 1004 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
29
30
31
32
33
34
35
36
import java.util.*;
import java.io.*;
class Main {
public static String CaesarCipher(String str, int num) {
// code goes here
int code, newCode;
String result = "", newChar;
for (int i = 0; i < str.length(); i++) {
code = (int) str.charAt(i);
newCode = code + num;
if (code >= 65 && code <= 90) {
if (newCode > 90) newCode = 64 + (newCode - 90);
}
else if (code >= 97 && code <= 122) {
if (newCode > 122) newCode = 96 + (newCode - 122);
}
else {
newCode = code;
}
newChar = String.valueOf((char) newCode);
result += newChar;
}
return result;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(CaesarCipher(s.nextLine()));
}
}