java中的ROT-13函数?
作为标准Java库的一部分,是否已经有了java中的ROT-13函数?,java,rot13,Java,Rot13,作为标准Java库的一部分,是否已经有了rot13()和unrot13()实现?还是我必须自己写“重新发明轮子” 它可能看起来像这样: int rot13 ( int c ) { if ( (c >= 'A') && (c <= 'Z') ) c=(((c-'A')+13)%26)+'A'; if ( (c >= 'a') && (c <= 'z') ) c=(((c-'a')+13)%26)+'a';
rot13()
和unrot13()
实现?还是我必须自己写“重新发明轮子”
它可能看起来像这样:
int rot13 ( int c ) {
if ( (c >= 'A') && (c <= 'Z') )
c=(((c-'A')+13)%26)+'A';
if ( (c >= 'a') && (c <= 'z') )
c=(((c-'a')+13)%26)+'a';
return c;
}
introt13(intc){
如果((c>='A')&&(c='A')&&(c我不认为默认情况下它是Java的一部分,但下面是一个如何实现它的示例
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();
}
}
公共类Rot13{
公共静态void main(字符串[]args){
字符串s=args[0];
对于(int i=0;i 如果(c>='a'&&c='a'&&c='n'&&c='n'&&c也可以贡献我的功能来节省其他开发人员宝贵的时间
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();
}
公共静态字符串rot13(字符串输入){
StringBuilder sb=新的StringBuilder();
对于(int i=0;i 如果(c>='a'&&c='a'&&c='n'&&c='n'&&c,这里是我在字符串中移位字符的解决方案
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;
}
publicstaticvoidmain(字符串[]args){
字符串输入=“melike”;
int shiftCount=13;
char[]encryptedChars=encryptedChars(移位计数);
System.out.println(新字符串(encryptedChars));
char[]decryptedChars=decryptedChars(shiftCount);
System.out.println(新字符串(decryptedChars));
字符串加密=加密(输入,移位计数);
System.out.println(加密);
字符串解密=解密(加密,移位计数);
System.out.println(已解密);
System.out.println(input.equals(解密));
}
私有静态字符串解密(字符串输入,int shiftCount){
char[]decryptedChars=decryptedChars(shiftCount);
char[]chars=input.toCharArray();
char[]newChars=新字符[chars.length];
for(int i=0;i
@BNL我个人更喜欢rot104。需要专用协处理器though@BNL哈哈,谁说这是出于安全目的?顺便说一句,rot13是它自己的逆函数,所以你实际上不需要“unrot13”方法。谢谢你花时间把它放到一个方法中。你也复制了吗?