Brief Explanation
So I know this is a rather easy problem to solve but for some reason I'm confused by what it's asking me so I really just need an explanation to it more than anything.
Problem
Read a word from the user and display the string with the letters shifted to the right by two positions and with the letters shifted to the left by two positions in the string. Save all of the three strings in separate variables and display all three of them at the end of the program.
Confusion
Here's how I'm confused because I don't really understand what would shifting the letters do considering the fact that if there all being shifted the same distance in the same direction then the word you should get is the original then isn't it? I'm might be completely wrong in this. I'm also sure I've done this before but I can find the older file for it and I'm confused for some reason.
Shifting a set of letters can be interpreted in two ways that I know of. Lets look at an example string of '"abcdef"'. This could be left shift by two to produce the string '"cdefab"'. This the characters wrap around. The other shift would produce '"cdef"'. This is equivalent to shifting bytes where information is lost. The latter is done by deleting the first two characters, so I would guess this is not what is intended. Therefore, the intended output would be
Original: abcdef
Right Shifted: efabcd
Left Shifted: cdefab
I solved it Problem
Read a word from the user and display the string with the letters shifted to the right by two positions and with the letters shifted to the left by two positions in the string. Save all of the three strings in separate variables and display all three of them at the end of the program.
import java.util.Scanner;
public class StringShiftTwoLeftThenRight
{
public static void main(String[] args)
{
String word, rightShift = "", leftShift = "";
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter a word: ");
word = keyboard.nextLine();
rightShift = (word.substring((0),
(word.length()-2)));
leftShift = (word.substring((2),(word.length())));
System.out.println("\nThe String shifted two to right looks like this: " + rightShift);
System.out.println("\nThe String shifted two to left looks like this: " + leftShift);
System.out.println("\nThe String as it is looks like: " + word);
}
}