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 ?
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";
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
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.
#include <iostream>
#include <string>
usingnamespace 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;
}