首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ROT-13在java中的功能?

ROT-13在java中的功能?
EN

Stack Overflow用户
提问于 2019-03-25 23:39:35
回答 2查看 0关注 0票数 0

有没有已经是rot13()unrot13()实现为标准Java库的一个组成部分?或者我是否必须自己编写并“重新发明wheel”?

它可能看起来像这样:

代码语言:javascript
运行
复制
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; 
}
EN

回答 2

Stack Overflow用户

发布于 2019-03-26 07:54:14

我不认为它默认是Java的一部分,但这里有一个如何实现它的例子;

代码语言:javascript
运行
复制
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

票数 0
EN

Stack Overflow用户

发布于 2019-03-26 09:28:31

这是我在字符串中转换字符的解决方案。

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档