|
@@ -0,0 +1,58 @@
|
|
1
|
+import static java.lang.Character.isLowerCase;
|
|
2
|
+import static java.lang.Character.isUpperCase;
|
|
3
|
+import static java.lang.Character.toLowerCase;
|
|
4
|
+
|
|
5
|
+public class ROT13 {
|
|
6
|
+
|
|
7
|
+ ROT13(Character cs, Character cf) {
|
|
8
|
+ }
|
|
9
|
+
|
|
10
|
+ ROT13() {
|
|
11
|
+ }
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+ public String crypt(String text) throws UnsupportedOperationException {
|
|
15
|
+
|
|
16
|
+ return encrypt(text);
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ //Turn string into char array, and iterate through it.
|
|
20
|
+ //Separate lower and uppercase letter.
|
|
21
|
+ //if letter is above M, subtract 13 letters, otherwise add 13 letters.
|
|
22
|
+ //return as new string.
|
|
23
|
+ public String encrypt(String text) {
|
|
24
|
+ char[] texts = text.toCharArray();
|
|
25
|
+ for(int i = 0; i < texts.length; i++) {
|
|
26
|
+ char letter = texts[i];
|
|
27
|
+ if(letter >= 'a' && letter <= 'z') {
|
|
28
|
+ if(letter > 'm') {
|
|
29
|
+ letter -= 13;
|
|
30
|
+ } else {
|
|
31
|
+ letter += 13;
|
|
32
|
+ }
|
|
33
|
+ } else if (letter >= 'A' && letter <= 'Z') {
|
|
34
|
+ if (letter > 'M') {
|
|
35
|
+ letter -= 13;
|
|
36
|
+ } else {
|
|
37
|
+ letter += 13;
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+ texts[i] = letter;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ return new String(texts);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public String decrypt(String text) {
|
|
47
|
+ return encrypt(text);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ //set s as c and return as new substring and append it.
|
|
51
|
+ //then append a subsequence of the specified charsequence to this sequence.
|
|
52
|
+ //convert to string then return.
|
|
53
|
+ public static String rotate(String s, Character c) {
|
|
54
|
+ StringBuilder string = new StringBuilder();
|
|
55
|
+ return string.append(s.substring(s.indexOf(c))).append(s, 0, s.indexOf(c)).toString();
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+}
|