|
@@ -1,32 +1,49 @@
|
1
|
|
-import static java.lang.Character.isLowerCase;
|
2
|
|
-import static java.lang.Character.isUpperCase;
|
3
|
|
-import static java.lang.Character.toLowerCase;
|
4
|
|
-
|
5
|
1
|
public class ROT13 {
|
6
|
2
|
|
|
3
|
+ private int shift;
|
|
4
|
+
|
7
|
5
|
ROT13(Character cs, Character cf) {
|
|
6
|
+ shift = cf - cs;
|
8
|
7
|
}
|
9
|
8
|
|
10
|
9
|
ROT13() {
|
|
10
|
+ this.shift = 13;
|
11
|
11
|
}
|
12
|
12
|
|
13
|
|
-
|
14
|
13
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
|
-
|
16
|
|
- return "";
|
|
14
|
+ return encrypt(text);
|
17
|
15
|
}
|
18
|
16
|
|
19
|
17
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
18
|
+ StringBuilder encrypted = new StringBuilder();
|
|
19
|
+ for (int i = 0; i < text.length(); i++) {
|
|
20
|
+ if (Character.isLowerCase(text.charAt(i)))
|
|
21
|
+ encrypted.append((char) ('a' + ( (text.charAt(i) - 'a') + shift) % 26));
|
|
22
|
+ else if (Character.isUpperCase((text.charAt(i))))
|
|
23
|
+ encrypted.append((char) ('A' + ( (text.charAt(i) - 'A') + shift) %26));
|
|
24
|
+ else encrypted.append(text.charAt(i));
|
|
25
|
+ System.out.println(encrypted);
|
|
26
|
+ }
|
|
27
|
+ return encrypted.toString();
|
21
|
28
|
}
|
22
|
29
|
|
23
|
30
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
31
|
+ StringBuilder encrypted = new StringBuilder();
|
|
32
|
+ for (int i = 0; i < text.length(); i++) {
|
|
33
|
+ if (Character.isLowerCase(text.charAt(i)))
|
|
34
|
+ encrypted.append((char) ('a' + ( (text.charAt(i) - 'a') + (26 - shift)) % 26));
|
|
35
|
+ else if (Character.isUpperCase((text.charAt(i))))
|
|
36
|
+ encrypted.append((char) ('A' + ( (text.charAt(i) - 'A') + (26 - shift)) %26));
|
|
37
|
+ else encrypted.append(text.charAt(i));
|
|
38
|
+ }
|
|
39
|
+ return encrypted.toString();
|
25
|
40
|
}
|
26
|
41
|
|
27
|
42
|
public static String rotate(String s, Character c) {
|
28
|
|
-
|
29
|
|
- return "";
|
|
43
|
+ int index = s.indexOf(c);
|
|
44
|
+ String beginning = s.substring(index, s.length());
|
|
45
|
+ String end = s.substring(0, index);
|
|
46
|
+ return beginning + end;
|
30
|
47
|
}
|
31
|
48
|
|
32
|
49
|
}
|