Palindrome with phrases

For some reason my program only works if enter phrases. when i put in a single word, it terminates

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
#include <iostream>
#include<string>
using namespace 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::cout << "Enter your string: \n";
    std::string myString;
    getline(std::cin, myString);
    myString.erase(std::remove_if(myString.begin(), myString.end(), isspace), myString.end());
    std::cout << myString << '\n';
}
Last edited on
closed account (48T7M4Gy)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

int main()
{
    size_t pos=0;
    bool is_a_palindrome = true;
    std::string line, word;
    
    std::cout<<"Enter a word to check if it is a Palindrome: ";
    
    getline(std::cin, line);
    
    do{
        pos = line.find_first_of(' ');
        word = line.substr(0, pos);
        line.erase(0, pos + 1);
        
        is_a_palindrome = true;
        
        for(int i = 0; i < word.length()/2 + 1; i++ )
        {
            if ( word[i] != word[ word.length() - i - 1] )
            {
                is_a_palindrome = false;
                break;
            }
        }
        
        std::cout << word << ' ';
        if( is_a_palindrome )
            std::cout << " is";
        else
            std::cout << " is not";
        
        std::cout << " a palindrome\n";
        
    }while( pos != std::string::npos);
    
    return 0;
}
Last edited on
(Deliberately) ignores upper/lower case and any blanks, punctuation etc.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <cctype>
using namespace 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] ) ) return false;   // check for difference, regardless of case

      istart++;                                                         // contract from two ends
      iend--;
   }
   return true;                                                         // 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
Last edited on
Topic archived. No new replies allowed.