Skip to content

Commit 46ab96f

Browse files
committed
issue #26 Caesar Cipher java
1 parent 2059f8d commit 46ab96f

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/hackerrank/CaesarCipher.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package hackerrank;
2+
3+
public class CaesarCipher {
4+
5+
static String caesarCipher(String s, int k) {
6+
7+
StringBuffer result = new StringBuffer();
8+
9+
for (int i = 0; i < s.length(); i++) {
10+
//대문자
11+
if (Character.isUpperCase(s.charAt(i))) {
12+
char ch = (char) (((int) s.charAt(i) + k - 65) % 26 + 65);
13+
result.append(ch);
14+
//소문자
15+
} else if(Character.isLowerCase(s.charAt(i))) {
16+
char ch = (char) (((int) s.charAt(i) + k - 97) % 26 + 97);
17+
result.append(ch);
18+
//기타 특수문자등
19+
} else {
20+
result.append(s.charAt(i));
21+
}
22+
}
23+
return result.toString();
24+
}
25+
26+
public static void main(String[] args) {
27+
System.out.println(caesarCipher("a-z", 2) + ", ans: c-b");
28+
//System.out.println(caesarCipher("middle-Outz", 2) + ", ans: okffng-Qwvb");
29+
//System.out.println(caesarCipher("Hello_World!", 4) + ", ans: Lipps_Asvph!");
30+
//System.out.println(caesarCipher("www.abc.xy", 87) + ", ans: fff.jkl.gh");
31+
}
32+
}

0 commit comments

Comments
 (0)