Caesar Cipher程序,如何只使用字符a-z?

时间:2018-02-06 15:37:10

标签: java string caesar-cipher

Caesar Cipher单独应用于字符串中的每个字母。每个字母必须向前移动字母表中的n个步骤。如果一个字母从字母表的末尾移开('z'),那么它会一直向后移动到字母表的开头('a')。 `

import java.util.*;

class question7{

    public static void main (String[] args ){

        String str = "";
        //allowing program to take user input using the keyboard
        Scanner kb = new Scanner(System.in);
        Scanner s = new Scanner(System.in);

        int n = 0;
        System.out.println("increasing the letters in string by n");

        while (true){
            System.out.println("Please enter your string");
            str = kb.nextLine(); 

            System.out.println("Please enter your n value");
            n = s.nextInt(); 

            String incrementedword=new String();
            for (int i=0;i<str.length();i++){
                incrementedword+=(char)(str.charAt(i)+n);
            }

            System.out.println ("your word is "+incrementedword);

        }
    }
}

例如,以下输入(“hello world”,1)应该返回“ifmmp xpsme”

但是当我键入(“hello world”,1)时输出为“ifmmp!xpsme”

我做错了什么?

4 个答案:

答案 0 :(得分:4)

这里有两个问题:

  • 在将一些字符增加到接近字母结尾的位置后,您将获得非字母数字字符(z + 1将成为大括号 {,例如 - 请参阅ASCII table) 。尝试使用余数运算符(%)。例如,117%100将是17,13%3将是1.这种方式(x + 1)%10永远不会超过10,将从0开始计数,随着x的增加再次开始计数。
  • 如果您只想加密a..z和A..Z范围内的字符,请排除其他字符。简单比较有效:if(x&gt; ='a'&amp;&amp; x&lt; ='z')。

我不会在这里提供完整且有效的代码,因为它看起来像是作业。

答案 1 :(得分:0)

如果字母不在范围内,您将需要控制环绕。对于在字母z后面移位的字母,要进行环绕。下面的代码只会移动字符串中的字母。

import java.util.*;

class question7{

public static void main (String[] args ){

    String str = "";
    //allowing program to take user input using the keyboard
    Scanner kb = new Scanner(System.in);
    Scanner s = new Scanner(System.in);

    int n = 0;
    int a = 'a';
    int z = 'z';

    System.out.println("increasing the letters in string by n");

      while (true){
        System.out.println("Please enter your string");
        str = kb.nextLine().toLowerCase(); 

        System.out.println("Please enter your n value");
        n = s.nextInt(); 

        int newCharValue;
        int currentChar;

        String incrementedword=new String();
        for (int i=0;i<str.length();i++){

          currentChar = str.charAt(i);

          if(!Character.isLetter(currentChar)){
            incrementedword+=currentChar;
            continue;
          }

          newCharValue = currentChar + n;

          if(newCharValue <= z)
            incrementedword+=(char)newCharValue;
          else
            incrementedword+=(char)(a + (newCharValue - a + 1)%26);
        }
        System.out.println ("your word is "+incrementedword);
      }
 }
}

答案 2 :(得分:0)

您可以将字母转换为ascii,然后以这种方式递增它们吗?

我的意思是,你必须设置约束,&#39; a&#39;是97岁,&#39; z&#39;这将确保它保持在小写字符内,你可以告诉它循环回到&#39; a&#39;。

答案 3 :(得分:0)

在循环中放置一个条件,该条件将检查当前字符是否从字母“ a”到“ z”的小写字母:

用于检查当前字符是否为小写字母(a到z)的代码段:

if (currentChar >= 'a' && currentChar <= 'z') {
//Process the shifting of the characters here
}

现在可以将每个字母向前移动n个字母,请尝试以下公式:

if (currentChar >= 'a' && currentChar <= 'z') {
   currentChar = (char) ((currentChar - 'a' + k) % 26 + 'a');
//Where n is the number of shifts forward (ex: a + 2 = c)
}
相关问题