Java 中的 ROT-N(或 ROT-X)函数

ROT-N (or ROT-X) function in Java

目前,我正在编写一个程序,该程序使用 Java 在给定的字符串上执行 ROT-1 直到并包括 ROT-25。在我的研究开始时,我发现 this 代码:

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;
        StdOut.print(c);
    }
    StdOut.println();
}
}

在解决了一些问题后,我得到了这个:

private static void rotALL(String input) {
//Loop 25 times, starting with ROT-1 and ending at ROT-25 (every possibliity besides the original input)
    for (int i = 1; i < 26; i++) {
        int rot = 26 - i;
        System.out.print("ROT" + rot + ": ");    
        for (int charIndex = 0; charIndex < input.length(); charIndex++) {
            char c = input.charAt(charIndex);

            int inta = 97; //a in the ASCII table
            int intaWithRot = inta + rot;
            int intA = 65; //A in the ASCII table
            int intAWithRot = intA + rot;

            int intaWithRotPlusOne = intaWithRot + 1;
            int intaWithRotPlusi = intaWithRot + i;
            int intAWithRotPlusOne = intAWithRot + 1;
            int intAWithRotPlusi = intAWithRot + i;

            if (c >= inta && c <= intaWithRot) {
                c += rot;
           } else if (c >= intA && c <= intAWithRot) {
                c += rot;
            } else if (c >= intaWithRotPlusOne && c <= intaWithRotPlusi) {
                c -= rot;
            } else if (c >= intAWithRotPlusOne && c <= intAWithRotPlusi) {
                c -= rot;
            }
            System.out.print(c);
        }
        System.out.println();
    }

现在我要解决的问题:

  1. 当我使用 ROT-13 输入 "grfg qngn",即 "test data" 时,我对 ROT-13 的输出是 "ROT13: test d{t{",“{”和"a"在ASCII table中相距26位,但我不知道为什么会出现此错误,当"e"等字母正确显示时。

  2. 如何更改此算法,使其通过 ROT-1 循环到 ROT-25?我认为这应该可以解决问题,但我遗漏了一些东西。

在此先致谢并致以亲切的问候!

解决此问题的方法有数百万种,作为学习者,您应该探索所有方法。我对使用模“%”运算符的评论可以用这个小方法来说明:

private static char rotateLower(char c, int rot) {
    int baseBand = c - 'a';
    int modified = (baseBand + rot) % 26;
    return (char) (modified + 'a');
}
/**
 * Returns a list with Strings which are rotated ROT-n. n = 26 - listIndex
 *
 * Source: http://www.rot-n.com/?page_id=4 
 *
 * @param input the string to mutate
 * @param numeric include numeric values
 * @return a list with mutated strings
 */
private static List<String> rotN(String input, boolean numeric) {
    List<String> output = new ArrayList<>();
    for (int n = 0; n < 26; n++) {
        String result = "";
        int length = input.length();

        for (int i = 0; i < length; i++) {
            char ascii = input.charAt(i);
            char rotated = ascii;
            //Capital letters are 60 to 90
            if (ascii > 64 && ascii < 91) {
                rotated = (char) (rotated + n);
                if (rotated > 90) {
                    rotated += -90 + 64;
                }
                if (rotated < 65) {
                    rotated += -64 + 90;
                }
            } else if (ascii > 96 && ascii < 123) { //Lowercase letters are between 97 and 122
                rotated = (char) (rotated + n);
                if (rotated > 122) {
                    rotated += -122 + 96;
                }
                if (rotated < 97) {
                    rotated += -96 + 122;
                }
            }
            //Numeric values are between 48 to 57 
            if (numeric && ascii > 47 && ascii < 58) {
                rotated = (char) (rotated + n);
                if (rotated > 47) {
                    rotated += -57 + 47;
                }
                if (rotated < 58) {
                    rotated += -47 + 57;
                }
            }
            result += (char) rotated;
        }
        output.add(result);
    }
    return output;
}

这是我找到的解决方案,有效。