|
@@ -1,10 +1,10 @@
|
1
|
|
-import static java.lang.Character.isLowerCase;
|
2
|
|
-import static java.lang.Character.isUpperCase;
|
3
|
|
-import static java.lang.Character.toLowerCase;
|
4
|
1
|
|
5
|
2
|
public class ROT13 {
|
6
|
3
|
|
|
4
|
+ Integer diff;
|
7
|
5
|
ROT13(Character cs, Character cf) {
|
|
6
|
+ this.diff=Math.abs((int)cs-(int)cf);
|
|
7
|
+
|
8
|
8
|
}
|
9
|
9
|
|
10
|
10
|
ROT13() {
|
|
@@ -12,21 +12,41 @@ public class ROT13 {
|
12
|
12
|
|
13
|
13
|
|
14
|
14
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
|
-
|
16
|
|
- return "";
|
|
15
|
+ String str="";
|
|
16
|
+
|
|
17
|
+ for (int i=0; i<text.length(); i++)
|
|
18
|
+ {
|
|
19
|
+ if(!Character.isLetter(text.charAt(i)))
|
|
20
|
+ {
|
|
21
|
+ str+=text.charAt(i);
|
|
22
|
+ }
|
|
23
|
+ else if (Character.isUpperCase(text.charAt(i)))
|
|
24
|
+ {
|
|
25
|
+ char ch = (char)(((int)text.charAt(i) + diff - 65) % 26 + 65);
|
|
26
|
+ str+=ch;
|
|
27
|
+ }
|
|
28
|
+ else if(Character.isLowerCase(text.charAt(i)))
|
|
29
|
+ {
|
|
30
|
+ char ch = (char)(((int)text.charAt(i) + diff - 97) % 26 + 97);
|
|
31
|
+ str+=ch;
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ return str;
|
17
|
35
|
}
|
18
|
36
|
|
19
|
37
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
38
|
+
|
|
39
|
+ return crypt(text);
|
21
|
40
|
}
|
22
|
41
|
|
23
|
42
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
43
|
+ return crypt(text);
|
25
|
44
|
}
|
26
|
45
|
|
27
|
|
- public static String rotate(String s, Character c) {
|
28
|
|
-
|
29
|
|
- return "";
|
|
46
|
+ public static String rotate(String string, Character c)
|
|
47
|
+ {
|
|
48
|
+ Integer diff=(int)c-(int)'A';
|
|
49
|
+ return string.substring(diff,string.length())+string.substring(0,diff);
|
30
|
50
|
}
|
31
|
51
|
|
32
|
52
|
}
|