#include <iostream>
#include<string>
usingnamespace std;
int main(){
int x=0;
int length=0;
int pos=0;
string word;
string wordn;
int palindrome=0;
for (int r=0;r<3;r++){
palindrome=0;
cout<<"Enter a word to check if it is a Palindrome: ";
getline(cin,word);
do{
pos=word.find(" ");
word.erase((pos),1);
} while(word.find(" ")!=string::npos);
//cout<<word;
length=word.length();
do{
if(word[x] != word[length-x-1]){
palindrome=-1;
}
x++;
}while(palindrome==0 && x<(length));
if (palindrome==0){
cout<<"The word you entered is a palindrome";
cout<<endl;
}
else{
cout<<"The word you entered is not a Palindrome.";
cout<<endl;
}
}
return 0;
}
line 19: pos=word.find(" ")
there is no white-space within a single word std::string, neither is it null-terminated like a C-string. Try something like this to get rid of whitespaces and then you can run the usual palindrome checker:
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
//======================================================================
bool isPalindrome( string s )
{
int istart = 0, iend = s.size() - 1;
while ( istart < iend ) // work from opposite ends of string
{
while ( !isalpha( s[istart] ) && istart < iend ) istart++; // strip out blanks, punctuation etc.
while ( !isalpha( s[iend ] ) && iend > istart ) iend--; // any resulting case istart == iend dealt with OK below
if ( toupper( s[istart] ) != toupper( s[iend] ) ) returnfalse; // check for difference, regardless of case
istart++; // contract from two ends
iend--;
}
returntrue; // no failures found, so palindrome
}
//======================================================================
int main()
{
string text;
cout << "Input a word or phrase" << endl;
getline( cin, text );
cout << "\n\"" << text << "\"" << ( isPalindrome( text ) ? " IS " : " is NOT " ) << "a palindrome";
}
"lastchance" is NOT a palindrome
or Napoleon's rant:
"Able was I ere I saw Elba" IS a palindrome
or, from the brilliant Wikipedia article on palindromes:
"A man, a plan, a canal - Panama!" IS a palindrome