Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

620
Views
Shifting the characters in a string in Java

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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!