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.
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?– Maniero
bigown I know I should present with Cout, but I’m not knowing what else the code put on this last Cout.
– Paulo Roberto
I’ve made some improvements that might help you improve your code. I hope it helps you. See how it works on ideoone.
– Maniero
It will be cool when you improve to ignore accents, space and punctuation, then it becomes fully functional :)
– Maniero