Relating to this question: ROT-13 function in java?
What would be the code to decode rot13 in java? Do I simply reverse the signs?
Below is the code for encoding a String into rot13:
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();
}
}
You don't reverse the signs. The decoding method is identical to the encoding method.
For example : 'a' is encoded to 'n'. If you "encode" the 'n', it is decoded back to 'a'.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句