-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot13.java
28 lines (22 loc) · 860 Bytes
/
rot13.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
class Solution {
public static String rot13(String message) {
String alpha = "abcdefghijklmnopqrstuvwxyz";
StringBuilder newWord = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
char letter = message.charAt(i);
char lower = Character.toLowerCase(message.charAt(i));
if (alpha.contains(String.valueOf(lower))) {
int letterIndex = alpha.indexOf(lower);
char newLetter = alpha.charAt((letterIndex + 13) % 26);
if (Character.isUpperCase(letter)) {
newWord.append(Character.toUpperCase(newLetter));
} else {
newWord.append(newLetter);
}
} else {
newWord.append(letter);
}
}
return newWord.toString();
}
}