ROT-13在java中的功能? [英] ROT-13 function in java?
本文介绍了ROT-13在java中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否已经有一个 rot13()
和 unrot13()
实现作为标准Java库之一的一部分?或者我是否必须自己编写并重新发明轮子?
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 "reinvent the wheel"?
它可能看起来像这样:
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;
}
推荐答案
我不认为这是默认情况下是Java的一部分,但这是一个如何实现它的示例;
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();
}
}
来源:http://introcs.cs.princeton.edu/java/31datatype/Rot13.java.html
这篇关于ROT-13在java中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文