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.
This piece of code
String foo = "abcdefz";
String bar = "";
for (char c : foo.toCharArray()) {
bar += Character.toString((char) (((c - 'a' + 1) % 26) + 'a'));
}
System.out.println(bar);
will output
bcdefga
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à!
** EDIT **
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);
Will output
mnopqrl
The value of shiftValue
may be any positive integer (i.e. shifting -2 is the same as shifting 24). Try it.
** UPDATE **
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)));
Will output
l
hemmo wormd
** EDIT 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);
Naturally, this code will replace every char read from stdin in the text
string. An example of output would be :
$ ! e wo
hfllpaxprlda
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With