将字符串中的字母提升为java中的下一个字母 [英] Promoting letters in a string to the next letter in java

查看:326
本文介绍了将字符串中的字母提升为java中的下一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having issues figuring out how to get my code to increment the string that is given by user input so that when a user chooses to replace a letter like z it would go to a, b to c etc. The catch is I have to do this without using boolean. I am supposed to get this by using arithmetics to get the promotion from z to a from the users input. Plus must be only lower case letters from a-z. Any help would be appreciated thanks.

推荐答案

这段代码

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

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

System.out.println(bar);

将输出

bcdefga

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

What it does is take the character, substract the character code for 'a', thus given a value from 0 to 25. Then we increment 1. Take that answer and perform a modulus 26, so if we had 'z', we substract 'a' thus giving 25 + 1 = 26, modulus 26 = 0. We then add 'a' again and voilà!

** 编辑 **

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

You can even push the concept a little further and add a variable "shifting" value :

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);

将输出

mnopqrl

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

The value of shiftValue may be any positive integer (i.e. shifting -2 is the same as shifting 24). Try it.

** 更新 **

好吧,只需更换你的alpha + 1与等式。并不是说我想给你提供一切,但如果你必须坚持,这就是你需要做的事情:

Well, just replace your alpha+1 with the equation. Not that I want to feed you everything, however if you must insist, here is what you need to do :

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

** DISCLAIMER ** : contains your homework solution

// 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)));

将输出

l
hemmo wormd

** 编辑2 **

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

If you have an index-based alphabet (in case you need to include extra chars, etc.) here's another solution. There is no comment and no optimization, but the code works and should be self explanatory... FYI only :

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);

当然,这段代码将替换 stdin 中读取的每个char code> text string。输出的一个例子是:

Naturally, this code will replace every char read from stdin in the text string. An example of output would be :

$ ! e wo
hfllpaxprlda 

这篇关于将字符串中的字母提升为java中的下一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆