Caesar Cipher after the first space in a given String starts adding a char ( 'C' ) to the beginning of every word after that space

huangapple go评论88阅读模式
英文:

Caesar Cipher after the first space in a given String starts adding a char ( 'C' ) to the beginning of every word after that space

问题

我的凯撒密码在消息的每个单词之后的第一个单词之前添加了一个"C"。所以我不确定是否可能与我如何处理给定消息中的空格有关?

期望输出:

你的消息?
进攻黎明的虫群
编码密钥?
3
DWWDFN CHUJ DW GDZQ

当前输出:

你的消息?
进攻黎明的虫群
编码密钥?
3
DWWDFN CCHUJ CDW CGDZQ

   public static void rot() {
        
       char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l',
               'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};

        String rotM = "";

        Scanner scnr = new Scanner(System.in);
        System.out.print("你的消息? ");
        String message = scnr.nextLine();
        System.out.print("编码密钥? ");
        int key = scnr.nextInt();

       message = message.toLowerCase();

        for (int i = 0; i < message.length(); i++) {
            if (!Character.isLetter(message.charAt(i))) rotM += " ";
            int origNumIndex = new String(alphabet).indexOf(message.charAt(i));
            int newIndex = origNumIndex + key;
            if (newIndex > 25) newIndex = newIndex - 26;
            char newLetter = alphabet[newIndex];
            rotM += newLetter;
        }

        System.out.println(rotM.toUpperCase());
        
   }
英文:

My Caesar cipher is adding a C at the beginning of every word after the first word in the message I want to rot. So I'm not sure if maybe it has to do with how I'm handling the spaces in the given message?

Expected output:

Your message?
Attack zerg at dawn
Encoding key?
3
DWWDFN CHUJ DW GDZQ

Current output:

Your message?
Attack zerg at dawn
Encoding key?
3
DWWDFN CCHUJ CDW CGDZQ

   public static void rot() {
        
       char[] alphabet = {&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;i&#39;,&#39;j&#39;,&#39;k&#39;,&#39;l&#39;,
               &#39;m&#39;,&#39;n&#39;,&#39;o&#39;,&#39;p&#39;,&#39;q&#39;,&#39;r&#39;,&#39;s&#39;,&#39;t&#39;,&#39;u&#39;,&#39;v&#39;,&#39;w&#39;,&#39;x&#39;,&#39;y&#39;,&#39;z&#39;};

        String rotM = &quot;&quot;;

        Scanner scnr = new Scanner(System.in);
        System.out.print(&quot;Your message? &quot;);
        String message = scnr.nextLine();
        System.out.print(&quot;Encoding key? &quot;);
        int key = scnr.nextInt();

       message = message.toLowerCase();

        for (int i = 0; i &lt; message.length(); i++) {
            if (!Character.isLetter(message.charAt(i))) rotM += &quot; &quot;;
            int origNumIndex = new String(alphabet).indexOf(message.charAt(i));
            int newIndex = origNumIndex + key;
            if (newIndex &gt; 25) newIndex = newIndex - 26;
            char newLetter = alphabet[newIndex];
            rotM += newLetter;
        }

        System.out.println(rotM.toUpperCase());
        
   }

答案1

得分: 1

你忘记了一个 continue 语句。没有这个 continue,它会尝试对一个未知的索引进行编码。我添加了一个容错机制,并对你的代码进行了一些修复和改进:

public static void rot() {
    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    String alphabetString = new String(alphabet);

    StringBuilder rotM = new StringBuilder();

    Scanner scnr = new Scanner(System.in);
    System.out.print("你的消息? ");
    String message = scnr.nextLine();
    System.out.print("编码密钥? ");
    int key = scnr.nextInt();

    message = message.toLowerCase();

    for (int i = 0; i < message.length(); i++) {
        char ch = message.charAt(i);

        if (!Character.isLetter(ch)) {
            rotM.append(" ");
            continue; // <- 这个语句之前缺失
        }

        int index = alphabetString.indexOf(ch);

        if (index == -1)
            throw new IllegalStateException("非法字符 '" + ch + "'");

        rotM.append(alphabet[(index + key) % alphabet.length]);
    }

    System.out.println(rotM.toString().toUpperCase());

}
英文:

You forget a continue-statement. Without this continue, it tries to encode an unknown index. I added a failsafe, fixed and improved your code a bit:

public static void rot() {
char[] alphabet = {&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;,
&#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;};
String alphabetString = new String(alphabet);
StringBuilder rotM = new StringBuilder();
Scanner scnr = new Scanner(System.in);
System.out.print(&quot;Your message? &quot;);
String message = scnr.nextLine();
System.out.print(&quot;Encoding key? &quot;);
int key = scnr.nextInt();
message = message.toLowerCase();
for (int i = 0; i &lt; message.length(); i++) {
char ch = message.charAt(i);
if (!Character.isLetter(ch)) {
rotM.append(&quot; &quot;);
continue; // &lt;- This statement was missing
}
int index = alphabetString.indexOf(ch);
if (index == -1)
throw new IllegalStateException(&quot;Illegal character &#39;&quot; + ch + &quot;&#39;&quot;);
rotM.append(alphabet[(index + key) % alphabet.length]);
}
System.out.println(rotM.toString().toUpperCase());
}

huangapple
  • 本文由 发表于 2020年10月23日 04:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定