ROT-13 function in java?

前端 未结 3 975
野性不改
野性不改 2020-11-30 14:40

Is there already a rot13() and unrot13() implementation as part of one of the standard Java libraries? Or do I have to write it myself and \"reinv

3条回答
  •  误落风尘
    2020-11-30 15:23

    I don't think it's part of Java by default, but here's an example of how you can implement it;

    public class Rot13 { 
    
        public static void main(String[] args) {
            String s = args[0];
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if       (c >= 'a' && c <= 'm') c += 13;
                else if  (c >= 'A' && c <= 'M') c += 13;
                else if  (c >= 'n' && c <= 'z') c -= 13;
                else if  (c >= 'N' && c <= 'Z') c -= 13;
                System.out.print(c);
            }
            System.out.println();
        }
    
    }
    

    Source: http://introcs.cs.princeton.edu/java/31datatype/Rot13.java.html

提交回复
热议问题