forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
32 lines (27 loc) ยท 1008 Bytes
/
CaesarCipher.java
File metadata and controls
32 lines (27 loc) ยท 1008 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
package hackerrank;
public class CaesarCipher {
static String caesarCipher(String s, int k) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
//๋๋ฌธ์
if (Character.isUpperCase(s.charAt(i))) {
char ch = (char) (((int) s.charAt(i) + k - 65) % 26 + 65);
result.append(ch);
//์๋ฌธ์
} else if(Character.isLowerCase(s.charAt(i))) {
char ch = (char) (((int) s.charAt(i) + k - 97) % 26 + 97);
result.append(ch);
//๊ธฐํ ํน์๋ฌธ์๋ฑ
} else {
result.append(s.charAt(i));
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(caesarCipher("a-z", 2) + ", ans: c-b");
//System.out.println(caesarCipher("middle-Outz", 2) + ", ans: okffng-Qwvb");
//System.out.println(caesarCipher("Hello_World!", 4) + ", ans: Lipps_Asvph!");
//System.out.println(caesarCipher("www.abc.xy", 87) + ", ans: fff.jkl.gh");
}
}