|
@@ -1,9 +1,8 @@
|
1
|
1
|
import java.util.*;
|
2
|
2
|
|
3
|
3
|
public class ROT13 {
|
4
|
|
- int shift;
|
5
|
|
- Map<Character, Character> cypher;
|
6
|
|
- String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
4
|
+ private int shift;
|
|
5
|
+ private Map<Character, Character> cypher = new HashMap<>();
|
7
|
6
|
|
8
|
7
|
ROT13(Character cs, Character cf) {
|
9
|
8
|
shift = Math.abs((int)cs - (int)cf);
|
|
@@ -16,28 +15,25 @@ public class ROT13 {
|
16
|
15
|
}
|
17
|
16
|
|
18
|
17
|
public void setupHM() {
|
19
|
|
- cypher = new HashMap<>();
|
|
18
|
+ String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
19
|
+
|
20
|
20
|
for(Character c : alphabet.toCharArray()) {
|
21
|
21
|
int offset = (int)c + shift > 122 ? (int)c - shift : (int)c + shift;
|
22
|
22
|
cypher.put(c, (char)offset);
|
23
|
23
|
}
|
24
|
24
|
}
|
25
|
25
|
|
26
|
|
-
|
27
|
26
|
public String crypt(String text) throws UnsupportedOperationException {
|
28
|
27
|
StringBuilder result = new StringBuilder();
|
29
|
28
|
|
30
|
29
|
for(Character c : text.toCharArray()) {
|
31
|
30
|
if (Character.isLetter(c)) {
|
32
|
31
|
if (Character.isUpperCase(c)) {
|
33
|
|
- //get the cyphered lowercase char then add it to the result as uppercase.
|
34
|
32
|
result.append(Character.toUpperCase(cypher.get(Character.toLowerCase(c))));
|
35
|
33
|
}
|
36
|
34
|
else result.append(cypher.get(c));
|
37
|
35
|
}
|
38
|
|
- else {
|
39
|
|
- result.append(c);
|
40
|
|
- }
|
|
36
|
+ else result.append(c);
|
41
|
37
|
}
|
42
|
38
|
return result.toString();
|
43
|
39
|
}
|
|
@@ -51,13 +47,7 @@ public class ROT13 {
|
51
|
47
|
}
|
52
|
48
|
|
53
|
49
|
public static String rotate(String s, Character c) {
|
54
|
|
- String[] rotator = s.split("");
|
55
|
|
- StringBuilder result = new StringBuilder();
|
56
|
|
-
|
57
|
|
- for(int i = 0; i < rotator.length; i++) {
|
58
|
|
- result.append(rotator[(s.indexOf(c) + i) % rotator.length]);
|
59
|
|
- }
|
60
|
|
- return result.toString();
|
|
50
|
+ return s.substring(s.indexOf(c)) + s.substring(0, s.indexOf(c));
|
61
|
51
|
}
|
62
|
52
|
|
63
|
53
|
}
|