#include <iostream>
using namespace std;
int main()
{
cout << "Enter a word to test if its a palidrome: ";
string word;
cin >> word;
string Reverse;
for (int x=word.size()-1; x>=0; x--)
{
Reverse = Reverse + word[x];
if (word == 0)
cout <<"The word is a palindrome."<<endl;
else
cout <<"The word is not a palindrome."<<endl;
}
cout <<Reverse ;
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
bool isPalidrome = true;
cout << "Enter a word to test if its a palidrome: ";
string word;
cin >> word;
cin.ignore();
string Reverse;
for (int x = word.size() - 1; x >= 0; x--)
{
Reverse = Reverse + word[x];
}
for (int i = 0; i < word.size() - 1; i++)
{
if (word[i] != Reverse[i])
{
isPalidrome = false;
}
}
if (isPalidrome)
cout << word << " is a palidrome";
else
cout << word << " is not a palidrome";
cin.ignore();
return 0;
}
#include <algorithm>
#include <iostream>
#include <string> // missing in your code
// ...
std::string word;
std::cin >> word;
std::string reversed_word(word); // copy the word
std::reverse(reversed_word.begin(), reversed_word.end()); // reverse it
if (word == reversed_word)
// say it is a palindrome, etc.
#include <algorithm>
#include <iostream>
#include <string> // missing in your code
// ...
std::string word;
std::string reversed_word;
std::cin >> word;
reversed_word.resize(word.length()); // must resize to fit the contents
std::reverse_copy(word.begin(), word.end(), reversed_word.begin());
if (word == reversed_word)
// say it is a palindrome, etc.
As for Catfish, he is right in general, but if this is for homework, you should be reinventing the wheel, because that is the point, to learn about problem solving in computer science. Otherwise, do what Catfish said.
Or a bit more elegantly, use std::reverse_copy() instead of std::reverse():
Or, since you're making a copy anyway, just construct a new string in place with the reversed original:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
// ...
std::string word;
std::cin >> word;
std::string reversed_word(word.rbegin(), word.rend()) ;
if (word == reversed_word)
// say it is a palindrome, etc.
Or compare the first half of the string with the reverse of the second half:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <string>
#include <algorithm>
// ...
std::string word;
std::cin >> word;
if ( std::equal( word.begin(), word.begin + word.size()/2, word.rbegin() )
// say it is a palindrome, etc.