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:

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay