package test;
public class ROT13 {
public static void main(String[] args) {
String s = "a123456789";
System.out.println(rot13(s)); // n678901234
System.out.println(rot13(rot13(s))); // a123456789
System.out.println(rot13(rot13(rot13(s)))); // n678901234
System.out.println(rot13(rot13(rot13(rot13(s))))); // a123456789
}
public static String rot13(String s) {
String rot13 = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// 英文字母使用 rot13
if (c >= 'a' && c <= 'm' || c >= 'A' && c <= 'M') {
c += 13;
}
else if (c >= 'n' && c <= 'z' || c >= 'N' && c <= 'Z') {
c -= 13;
}
// 數字依此類推用 rot5
else if (c >= '0' && c <= '4') {
c += 5;
}
else if (c >= '5' && c <= '9') {
c -= 5;
}
rot13 += c;
}
return rot13;
}
}
Last active
August 29, 2015 14:18
-
-
Save Liullen/1d7e46153f15d8417153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class ROT13 { | |
public static void main(String[] args) { | |
String s = "a123456789"; | |
System.out.println(rot13(s)); // n678901234 | |
System.out.println(rot13(rot13(s))); // a123456789 | |
System.out.println(rot13(rot13(rot13(s)))); // n678901234 | |
System.out.println(rot13(rot13(rot13(rot13(s))))); // a123456789 | |
} | |
public static String rot13(String s) { | |
String rot13 = ""; | |
for (int i = 0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
// 英文字母使用 rot13 | |
if (c >= 'a' && c <= 'm' || c >= 'A' && c <= 'M') { | |
c += 13; | |
} | |
else if (c >= 'n' && c <= 'z' || c >= 'N' && c <= 'Z') { | |
c -= 13; | |
} | |
// 數字依此類推用 rot5 | |
else if (c >= '0' && c <= '4') { | |
c += 5; | |
} | |
else if (c >= '5' && c <= '9') { | |
c -= 5; | |
} | |
rot13 += c; | |
} | |
return rot13; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment