Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

628
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda