将字符串中的字母提升为java中的下一个字母

CSD*_*988 4 java

我有问题弄清楚如何让我的代码增加用户输入给出的字符串,这样当用户选择替换像z这样的字母时,它会转到a,b到c等.捕获是我有的不使用布尔值来做到这一点.我应该通过使用算术来从用户输入获得从z到a的促销.加上必须只是az的小写字母.任何帮助将不胜感激.

Yan*_*hon 18

这段代码

String foo = "abcdefz";
String bar = "";

for (char c : foo.toCharArray()) {
   bar += Character.toString((char) (((c - 'a' + 1) % 26) + 'a'));
}

System.out.println(bar);
Run Code Online (Sandbox Code Playgroud)

将输出

bcdefga
Run Code Online (Sandbox Code Playgroud)

它的作用是取字符,减去'a'的字符代码,从而给出0到25之间的值.然后我们增加1.得到答案并执行模数26,所以如果我们有'z',我们减去'a'因此给出25 + 1 = 26,模数26 = 0.然后再添加'a'并vo!

**编辑**

你甚至可以进一步推动这个概念并添加一个变量"移位"值:

int shiftValue = 12;

String foo = "abcdefz";
String bar = "";

for (char c : foo.toCharArray()) {
   bar += Character.toString((char) (((c - 'a' + shiftValue) % 26) + 'a'));
}

System.out.println(bar);
Run Code Online (Sandbox Code Playgroud)

会输出

mnopqrl
Run Code Online (Sandbox Code Playgroud)

shiftValue可以是任何正整数(即,移位-2与移位24相同).试试吧.

**更新**

好吧,只需用等式替换你的alpha + 1即可.并不是说我想给你提供一切,但如果你必须坚持,这就是你需要做的:

**免责声明**:包含您的作业解决方案

// define some constants
char FIRST_LETTER = 'a';    // the first letter in the alphabet
int ALPHABET_SIZE = 26;     // the number of letters in the alphabet
int SHIFT_VALUE = 1;        // number of letters to shift

Scanner kb = new Scanner(System.in);
String second = "hello world";    // target string

String alphabet = kb.next();
// TODO: need to check if alphabet has at least one char and if it's in the range of a-z
char alpha = alphabet.charAt(0);   // just keep the first char in the input
System.out.println(second.replace(alpha, (char) (((alpha - FIRST_LETTER + SHIFT_VALUE) %  ALPHABET_SIZE ) + FIRST_LETTER)));
Run Code Online (Sandbox Code Playgroud)

会输出

l
hemmo wormd
Run Code Online (Sandbox Code Playgroud)

**编辑2**

如果您有一个基于索引的字母表(如果您需要包含额外的字符等),这是另一个解决方案.没有评论也没有优化,但代码有效且应该是自我解释的......仅限FYI:

int shiftValue = 1;
char[] alphabet = new char[] {
   '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','!',' '
};
boolean[] replace = new boolean[alphabet.length];

Scanner kb = new Scanner(System.in);
String text = "hello world !";

System.out.print("$ ");
String input = kb.nextLine().toLowerCase();

Arrays.fill(replace, false);
for (char c : input.toCharArray()) {
   int index = -1;
   for (int i=0; i<alphabet.length; i++) {
      if (alphabet[i] == c) {
         index = i;
         break;
      }
   }
   if (index >= 0) {
      replace[index] = true;
   }
}

for (int i=alphabet.length - 1; i>0; i--) {
   if (replace[i]) {
      text = text.replace(alphabet[i], alphabet[(i+shiftValue) % alphabet.length]);
   }
}
System.out.println(text);
Run Code Online (Sandbox Code Playgroud)

当然,这个代码将取代所有的字符读取标准输入text的字符串.输出的一个例子是:

$ ! e wo
hfllpaxprlda 
Run Code Online (Sandbox Code Playgroud)


小智 2

String s = "q";
char c = s.charAt(0);
c++;
//here you can handle cases like z->a
if (c=='z') c = 'a';

char[] chars = new char[1];
chars[0] = c;
String s1 = new String(chars);
Run Code Online (Sandbox Code Playgroud)