Thread: Palindrome program not working

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    Question Palindrome program not working

    What I'm trying to do is check if a string is a palindrome or not. I'm supposed to ignore all non-letter characters but it doesn't appear to be working. Any suggestions?

    For ex. When I type in "He lived as a devil, eh?" it tells me that it's not a palindrome even though it is.

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    
    using namespace std;
    
    
    int main()
    {
        char ch;
        string s;
        cin.get(ch);
        while (ch!='\n')
        {
            if(ispunct(ch) || isspace(ch) || isdigit(ch))
            {
                ch='\0';
            }
            else
            ch=tolower(ch);
            s+=ch;
            cin.get(ch);
        }
    
    
        if(s==string(s.rbegin(),s.rend())){
            cout<<s<<"\nis a palindrome.";
        }
        else
            cout<<s<<"\nis not a palindrome.";
    
    
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I had to look that up ,,

    Palindrome, Palindrome Words List, Longest Examples, Define Palindrome

    Palindrome List – Palindromes, Palindrome Words, Examples

    so what is the basis of comparison to find one? Because by taking a 'd' it says it is, then by using an example in that list I get nope.
    Code:
    userx@slackwhere:~/bin
    $ ./temp
    d
    d
    is a palindrome.userx@slackwhere:~/bin
    $ ./temp
    A car, a man, a maraca.
    helloacaramanamaraca
    is not a palindrome.userx@slackwhere:~/bin
    $
    Plus you have no lead in message telling the user what to do. If I did not read the code and what you said to do in your explanation in here then I'd be lost.

    id try flipping your if else around then run a few more different ones to see what that does.
    Code:
     if(s==string(s.rbegin(),s.rend()))
             cout<<s<<"\nis not a palindrome.\n";
      else     
             cout<<s<<"\nis a palindrome.\n";
    Code:
    userx@slackwhere:~/bin
    $ ./temp
    A car, a man, a maraca.
    acaramanamaraca
    is a palindrome.
    userx@slackwhere:~/bin
    it might be that return value that is causing this effect, I do not know what it is.
    Last edited by userxbw; 11-06-2017 at 10:11 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You are still inserting characters you don't want they are just all \0 now. It would be better to not insert them at all.
    Last edited by whiteflags; 11-06-2017 at 10:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome checker working but ...
    By sean_cantab in forum C Programming
    Replies: 12
    Last Post: 04-15-2016, 07:48 PM
  2. Need help with palindrome program
    By bellagomes21 in forum C Programming
    Replies: 2
    Last Post: 02-13-2016, 03:33 AM
  3. Need help , Palindrome program
    By amigoloko in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2005, 12:07 PM
  4. Palindrome program help please
    By hunter_04 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2004, 07:38 PM
  5. C++ Palindrome program help
    By hunter_04 in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 09:23 PM

Tags for this Thread