DEV Community

Beatriz Maciel
Beatriz Maciel

Posted on • Edited on

2 1

HackerRank #20 | String Reverse | 🇧🇷

Neste exercício é pedido que se verifique se uma palavra é um palíndromo ou não.
O passo a passo é o seguinte:

  • Fazer o Scanner de uma palavra.
  • Declarar uma String vazia (chamei de palindromo)
  • Fazer uma iteração que começa a ler do final do tamanho (length) da String A e vai diminuindo a leitura (i--) até ler todos os caracteres
  • Somar a String vazia palindromo com os caracteres lidos da String que foi escaneada.
  • Fazer um if que iguala palindromo com a String A (escaneada). Se forem iguais retorna "Yes", caso contrário retorna "No".

=========

O código final fica assim, dentro da main:

        Scanner sc = new Scanner(System.in);

        System.out.println("Digite uma palavra ou frase:");
        String A = sc.nextLine();
        String palindromo = "";

        for(int i = A.length() -1; i >= 0; i--){

            palindromo += A.charAt(i);

        }

        if(palindromo.equals(A)) {
            System.out.print("Yes");
        }
        else {
            System.out.print("No");

        }
        sc.close();
Enter fullscreen mode Exit fullscreen mode

=========

Referências

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay