palindrome test

as some of you may know especialy those who helped me I recently used this code to create a program that takes a word from user input and reverses it:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;


int main()
{
string word;
cout<<"Enter aword with no spaces : ";
cin >> word;
cout << "Your word is: "<< word << "\nYour reversed word is: ";
int size = word.length() -1;
while (size >= 0)
{
cout << word[size];
size--;

}
system ("PAUSE");

return 0;

now I am trying to buld upon it an make it test wheather the word the user inputs is palindromic where should I start ?
Start by reversing the string.
Hint: http://www.cplusplus.com/reference/string/string/rbegin/
Ive got the reversing done now all I need to know is how to compare a std::string to a reverse iterated string
Two objects of type std::string can be compared with operator ==. For example

1
2
3
4
5
6
7
8
9
std::string s1 = "abc";
std::string s2 = "abc";
std::string s3 = "abC';

if ( s1 == s2 ) std::cout << "s1 is equal to s2\n";
else std::cout << "s1 is not equal to s2\n";

if ( s1 == s3 ) std::cout << "s1 is equal to s3\n";
else std::cout << "s1 is not equal to s3\n"; 
Last edited on
I tried the == operator and got an error message saying
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion) 26
I do not see where you got this error. Please show your code.
this is my code
#include <iostream>
#include <cstring>
#include <string>
using namespace std;


int main()
{
string word;
string::reverse_iterator drow;
cout<<"Enter aword with no spaces : ";
cin >> word;
cout << "Your word is: "<< word << "\nYour reversed word is: ";
for (drow=word.rbegin(); drow < word.rend(); drow++)
{
cout << *drow << endl;
}
if (word == drow)
{
cout << "palindrome\n";
}
else
{
cout << "not a palindrome\n";
}


system ("PAUSE");

return 0;
}

this gives me a red line undr the == and that error I mentioned earlier
You are trying to compare an object of type std::string with an object of type std::string::iterator. It is obvios that they are different. You task is very simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;


int main()
{
   string word;

   cout << "Enter aword with no spaces : ";
   cin >> word;

   cout << "Your word is: " << word << endl;

   string reversed_word( word.rbegin(), word.rend() );

   cout << "Your reversed word is: " << reversed_word << endl;

   if ( word == reversed_word ) cout << "It is palindrome\n";
   else cout << "It is not a palindrome\n";

   system ("PAUSE");

   return 0;
} 
Last edited on
that fixed every thing thank you!!!
Topic archived. No new replies allowed.