|
@@ -13,20 +13,54 @@ public class ROT13 {
|
13
|
13
|
|
14
|
14
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
15
|
|
16
|
|
- return "";
|
|
16
|
+ return encrypt(text);
|
17
|
17
|
}
|
18
|
18
|
|
19
|
19
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
20
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
21
|
+
|
|
22
|
+ for (int i = 0; i < text.length(); i++) {
|
|
23
|
+ if(!Character.isLetter(text.charAt(i))){
|
|
24
|
+ stringBuilder.append(text.charAt(i));
|
|
25
|
+ } else if (Character.isLowerCase(text.charAt(i))) {
|
|
26
|
+ stringBuilder.append(rotateLowerCase(text.charAt(i)));
|
|
27
|
+ } else { stringBuilder.append(rotateUpperCase(text.charAt(i)));}
|
|
28
|
+ }
|
|
29
|
+ System.out.println(stringBuilder.toString());
|
|
30
|
+ return stringBuilder.toString();
|
21
|
31
|
}
|
22
|
32
|
|
23
|
33
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
34
|
+ return encrypt(text);
|
25
|
35
|
}
|
26
|
36
|
|
27
|
37
|
public static String rotate(String s, Character c) {
|
|
38
|
+ int indexToStart = 0;
|
|
39
|
+ for(int i = 0; i < s.length(); i++){
|
|
40
|
+ if(s.charAt(i) == c){
|
|
41
|
+ indexToStart = i;
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+ return createNewString(s, indexToStart);
|
|
45
|
+ }
|
28
|
46
|
|
29
|
|
- return "";
|
|
47
|
+ private static String createNewString(String s, int indexToStart) {
|
|
48
|
+ StringBuilder stringBuilder = new StringBuilder(s.substring(indexToStart));
|
|
49
|
+ stringBuilder.append(s.substring(0, indexToStart));
|
|
50
|
+ return stringBuilder.toString();
|
30
|
51
|
}
|
31
|
52
|
|
|
53
|
+ public static char rotateUpperCase(char c){
|
|
54
|
+ if (c + 13 <= 90){
|
|
55
|
+ return (char) (c + 13);
|
|
56
|
+ }
|
|
57
|
+ return (char) (c - 13);
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public static char rotateLowerCase(char c){
|
|
61
|
+ if (c + 13 <= 122){
|
|
62
|
+ return (char) (c + 13);
|
|
63
|
+ }
|
|
64
|
+ return (char) (c - 13);
|
|
65
|
+ }
|
32
|
66
|
}
|