Palindrome in C++

Asked

Viewed 3,148 times

3

The code below works, receives what was typed and stores it, only it does not make the logic to show whether it is palindromic or not and does not present on the screen the result for the user.

Please, where I went wrong and how to fix it?

#include <iostream>
using namespace std;

int main(void)
{

    string Texto = "";
    bool palindrono = true;
    int posicaoReversa = 0;

        cout << "Digite a palavra : " ;
        cin >> Texto;

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

        posicaoReversa = Texto.length() - i - 1;

        if (Texto.substr(i, 1) != Texto.substr(posicaoReversa, 1))
        {
            palindrono = false;
            break;
        }
    }
}

Recebe e armazena o valor, mas não mostra o resultado ao usuário

  • I did a test and it seems to be ok. Just does not present to the user? You are not ordering to present. You know you’d need to present the result at the end, probably with cout? The program won’t present itself if you don’t order it. You don’t know how to do this?

  • bigown I know I should present with Cout, but I’m not knowing what else the code put on this last Cout.

  • I’ve made some improvements that might help you improve your code. I hope it helps you. See how it works on ideoone.

  • It will be cool when you improve to ignore accents, space and punctuation, then it becomes fully functional :)

1 answer

5


All that was left was the presentation of the result, I only added a line at the end of the character check. I commented on some improvements you can make to your code and made them in example shown in ideone, especially consider cutting the time of the algorithm by half while avoiding comparing what has already been compared:

#include <iostream>
using namespace std;
int main(void) {
    string Texto = ""; //foi sempre usado camelcase, esta variável também deveria ser
    bool palindrono = true; //o nome da variável deveria ser palindromo
    int posicaoReversa = 0; //variável desnecessária
    cout << "Digite a palavra : ";
    cin >> Texto;
    //seria melhor pegar o tamanho do texto for do loop por questões de performance
    //também seria melhor comparar só até a metade, veja no ideone o exemplo modificado
    for (int i = Texto.length() - 1; i > 0; i--) {
        posicaoReversa = Texto.length() - i - 1; //não precisava colocar na variável
        //poderia usar o operador [] que já pega uma posição da string
        if (Texto.substr(i, 1) != Texto.substr(posicaoReversa, 1)) {
            palindrono = false;
            break;
        }
    }
    //terminou todo o processamento necessário, chegará aqui em duas situações:
    //1. ocorreu o break saindo do for e mandando para cá (não é palindromo)
    //2. terminou o loop do for e não tem mais o que repetir (é palindromo)
    cout << endl << (palindrono ? "É palindromo" : "Não é palindromo");
}

I did some code style enhancements and optimizations:

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

When you compare arara, For example, you only need to compare the last one with the first one and the second one with the second one. She originally performed three more unnecessary comparisons: she compared the middle one with herself, the second one with the penultimate one (which you had already compared) and the last one with the last one (which she had also compared). The code was not wrong, it could only be improved.

  • Thank you very much, I was putting it wrong even.

  • 1

    @Pauloroberto In addition to accepting an answer as certain, you already have a reputation to vote for all the answers that helped you. And vote on other people’s questions and answers as well. You can vote for everything. See [tour].

  • you are right, I still get lost a lot on this site, I always found that accepting how sure was the same as voting, I need to learn more about this site.

  • 1

    Accept you can only one answer in questions you have asked. Vote you can in any post, question, answer, from anyone, just can not vote in yours, of course :) At least where you are involved see where you left to vote and think you deserve the vote.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.