Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

621
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda