ROT-13 function in java?

前端 未结 3 960
野性不改
野性不改 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

    0 讨论(0)
  • 2020-11-30 15:32

    Might as well contribute my function to save other developers the valuable seconds

    public static String rot13(String input) {
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < input.length(); i++) {
           char c = input.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;
           sb.append(c);
       }
       return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-30 15:36

    Here is my solution to shift chars in a string.

    public static void main(String[] args) {
        String input = "melike";
        int shiftCount = 13;
        char[] encryptedChars = encryptedChars(shiftCount);
        System.out.println(new String(encryptedChars));
        char[] decryptedChars = decryptedChars(shiftCount);
        System.out.println(new String(decryptedChars));
        String encrypted = encrypt(input, shiftCount);
        System.out.println(encrypted);
        String decrypted = decrypt(encrypted, shiftCount);
        System.out.println(decrypted);
        System.out.println(input.equals(decrypted));
    }
    
    private static String decrypt(String input, int shiftCount) {
        char[] decryptedChars = decryptedChars(shiftCount);
        char[] chars = input.toCharArray();
        char[] newChars = new char[chars.length];
        for (int i = 0; i < chars.length; i++) {
            int diff = chars[i] - 'a';
            newChars[i] = decryptedChars[diff];
        }
        return new String(newChars);
    }
    
    private static String encrypt(String input, int shiftCount) {
        char[] encryptedChars = encryptedChars(shiftCount);
        char[] chars = input.toCharArray();
        char[] newChars = new char[chars.length];
        for (int i = 0; i < chars.length; i++) {
            int diff = chars[i] - 'a';
            newChars[i] = encryptedChars[diff];
        }
        return new String(newChars);
    }
    
    private static char[] encryptedChars(int shiftCount) {
        char[] newChars = new char[26];
        for (int i = 0; i < 26; i++) {
            newChars[i] = (char) ('a' + (i + shiftCount) % 26);
        }
        return newChars;
    }
    
    private static char[] decryptedChars(int shiftCount) {
        char[] newChars = new char[26];
        for (int i = 0; i < 26; i++) {
            newChars[i] = (char) ('a' + (i - shiftCount + 26) % 26);
        }
        return newChars;
    }
    
    0 讨论(0)
提交回复
热议问题