Skip to content

Instantly share code, notes, and snippets.

@Liullen
Last active August 29, 2015 14:18
Show Gist options
  • Save Liullen/1d7e46153f15d8417153 to your computer and use it in GitHub Desktop.
Save Liullen/1d7e46153f15d8417153 to your computer and use it in GitHub Desktop.
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;
}
}
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