【问题标题】:it will reverse the plain text, replace whitespaces with ‘*’ then shift the alphabets of the plain text according to the encryption key它将反转纯文本,用“*”替换空格,然后根据加密密钥移动纯文本的字母
【发布时间】:2021-10-06 16:59:35
【问题描述】:
public class pppp {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String s1;
        s1=input.nextLine();
        char ch[]=new char[s1.length()];
        int x;
        x=input.nextInt();
        int i=0;
        int j=0;
        int h=0;
        for(i=s1.length()-1;i>=0;i--) {
            ch[j]=s1.charAt(i);
            j++;
        }
        System.out.println(ch);
        for(i=0;i<s1.length();i++) {
            if(ch[i]==' ') {
                ch[i]='*';
            }
            else if((ch[i]+x)<122 && ch[i]!='*') {
                ch[i]+=x;
            }
            else if((ch[i]+x-'z')==1) {
                ch[i]='a';
            }
            else if(ch[i]+x-'z'>1) {
                ch[i]='a';
                ch[i]+=((ch[i]+x-'z')-1);
            }
        }
        
        System.out.println(ch);
    }
}

当我尝试打印“megadeth”并给出键 7 时,它会根据它移动字符来代替 t,这个 '{' 来了。

megadeth
7
htedagem
o{lkhnlt

它现在适用于 t 但如果我写 u 而不是 t 我会得到 N 这是错误的。 这是修改后的代码。

我能做些什么来解决它?

【问题讨论】:

  • 你认为应该打印什么?
  • a 而不是 {,因为 t+6 是 z,因为 t=116 在 ascii 值中,所以如果它超过 z,我希望它再次从 a 开始计数。
  • 听起来是个好主意。您将如何编写代码?
  • 我编辑了代码并写了这个,它适用于 megadeth 的 t 字符,但如果我写 megadeuh 我得到错误的答案。我用 N 代替你。
  • @ThorbjørnRavnAndersen 先生,您现在可以检查一下吗?

标签: java string encryption


【解决方案1】:

IIUC,你想:

  1. 反转你的字符串
  2. 将所有出现的空格字符 (' ') 替换为 '*'
  3. 对结果字符串的字母执行凯撒键移位

以下是使用流的方法:

import java.util.Scanner;
import java.util.stream.Collectors;

public class pppp {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String s1;
        s1=input.nextLine();
        int x;
        x=input.nextInt();

        System.out.println(
            new StringBuilder(s1)
                .reverse().toString()
                .chars()
                .map(c -> c == ' ' ? '*' : c)
                .map(c -> {
                    if (Character.isLetter(c)) {
                        char first = Character.isUpperCase(c) ? 'A' : 'a';
                        return first + (((c - first) + x) % 26);
                    }
                    else return c;
                })
                .mapToObj(c -> Character.toString((char) c))
                .collect(Collectors.joining())
        );
    }
}

这是计算移位的相关 sn-p:

char first = Character.isUpperCase(c) ? 'A' : 'a';
return first + (((c - first) + x) % 26);

假设您要将字母 t 移动 7 个位置。你的结果应该是a

为此,我们首先计算字母表中字母 t 的索引(从零开始):

index = 't' - 'a' = 116 - 97 = 19

接下来,将 key 添加到 index 模 26 以获取通过 key 移动的字母的索引。在这种情况下,键是 7。

shiftIndex = (index + 7) % 26 = (19 +7) % 26 = 26 % 26 = 0 

最后,您可以像这样计算移位的字符:

result = 'a' + shiftIndex = 'a' + 0 = 'a'

这就是我的解决方案所做的,加上大写字母的附加逻辑。

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多