|
@@ -3,45 +3,49 @@ import static java.lang.Character.isUpperCase;
|
3
|
3
|
import static java.lang.Character.toLowerCase;
|
4
|
4
|
|
5
|
5
|
public class ROT13 {
|
6
|
|
-
|
7
|
|
- private int rotation;
|
|
6
|
+ private int shift;
|
8
|
7
|
|
9
|
8
|
ROT13(Character cs, Character cf) {
|
10
|
|
- rotation = cf - cs;
|
|
9
|
+ shift = cf - cs;
|
11
|
10
|
// With this constructor, we are finding how much to rotate by
|
12
|
11
|
// UP TO CF, not including
|
13
|
12
|
}
|
14
|
13
|
|
15
|
14
|
ROT13() {
|
|
15
|
+ shift = 13;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public int getShift() {
|
|
19
|
+ return shift;
|
16
|
20
|
}
|
17
|
21
|
|
18
|
|
- public int getRotation() {
|
19
|
|
- return rotation;
|
|
22
|
+ public void setShift(int shift) {
|
|
23
|
+ this.shift = shift;
|
20
|
24
|
}
|
21
|
25
|
|
22
|
26
|
public String crypt(String text) throws UnsupportedOperationException {
|
23
|
|
- String s = "";
|
24
|
|
- // only 26 letters in the alphabet, with the 13 rotation, the crpt and encrypt are equal
|
|
27
|
+ StringBuilder sb = new StringBuilder();
|
|
28
|
+ // only 26 letters in the alphabet, with the 13 shift, the crpt and encrypt are equal
|
25
|
29
|
for (int i = 0; i < text.length(); i++) {
|
26
|
30
|
char c = text.charAt(i);
|
27
|
31
|
if (c >= 'a' && c <= 'm') {
|
28
|
|
- c += rotation;
|
29
|
|
- s += c;
|
|
32
|
+ c += shift;
|
|
33
|
+ sb.append(c);
|
30
|
34
|
} else if (c >= 'A' && c <= 'M') {
|
31
|
|
- c += rotation;
|
32
|
|
- s += c;
|
|
35
|
+ c += shift;
|
|
36
|
+ sb.append(c);
|
33
|
37
|
} else if (c >= 'n' && c <= 'z') {
|
34
|
|
- c -= rotation;
|
35
|
|
- s += c;
|
|
38
|
+ c -= shift;
|
|
39
|
+ sb.append(c);
|
36
|
40
|
} else if (c >= 'N' && c <= 'Z') {
|
37
|
|
- c -= rotation;
|
38
|
|
- s += c;
|
|
41
|
+ c -= shift;
|
|
42
|
+ sb.append(c);
|
39
|
43
|
} else {
|
40
|
|
- s += c;
|
|
44
|
+ sb.append(c);
|
41
|
45
|
}
|
42
|
46
|
}
|
43
|
47
|
// we are rotating each letter, not shifting
|
44
|
|
- return s;
|
|
48
|
+ return sb.toString();
|
45
|
49
|
}
|
46
|
50
|
|
47
|
51
|
public String encrypt(String text) {
|
|
@@ -63,9 +67,9 @@ public class ROT13 {
|
63
|
67
|
}
|
64
|
68
|
}
|
65
|
69
|
|
66
|
|
- int shift = index % s.length();
|
|
70
|
+ int rotation = index % s.length();
|
67
|
71
|
|
68
|
|
- return s.substring(shift) + s.substring(0, index);
|
|
72
|
+ return s.substring(rotation) + s.substring(0, index);
|
69
|
73
|
}
|
70
|
74
|
|
71
|
75
|
}
|