Thread: Palindrome emordnilaP

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Post Palindrome emordnilaP

    Program compiles fine. Does not make the correct decisions for palindrome determination. Show me the path to coding goodness;



    Entire code here.


    Suspect code here:

    Code:
    while(flag && ptr)
                 {
                     if(!strcmp(&temp[index], &ptr->letter))
                          
                         flag=false;
                     
                     ptr=ptr->next;
                     index++;
                 }
    Last edited by The Brain; 05-16-2005 at 02:38 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If oyu compare it letter by letter you don't need to use strcmp. Just compare the border letters then recursiverly (semantically speaking) check substring NOT containing these letters. This can be implemented using a simple loop:
    Code:
    bool IsPalindrome(const std::string& String)
    {
      int First = 0;
      int Last = static_cast<int>(String.size()) - 1;
    
      while(First < Last)
      {
        if(String[First] != String[Last]) return false;
        
        First++;
        Last--;
      }
    
      return true;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    int IsPalindrome( char* word, int length)
    {
       if( length <= 1) return 1;
       else
       return ( (word[0] == word[length-1]) && IsPalindrome(word+1,length-2));
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which naturally fails if you're testing anything other than a single word like "wow" "mom" "dad", etc. If you have any spaces, your test fails, unless it's perfectly aligned.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    mine?
    seems to work excellently. here try for yourself. cant get it to fail.

    Code:
    #include <iostream>
    #include <string>
    
    int IsPalindrome( const char* word, size_t length)
    {
       if( length <= 1) return 1;
       else
       return ( (word[0] == word[length-1]) && IsPalindrome(word+1,length-2));
    }
    
    int main()
    {
       std::string test;
       std::cout<<" enter string.... ";
       std::getline(std::cin,test);
       if (IsPalindrome(test.c_str(),test.length()))
          std::cout<<std::endl<<"palindrome"<<std::endl;
       else
          std::cout<<std::endl<<" not palindrome"<<std::endl;
       getchar();
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I think he refers to a string like:

    "ab cba"

    which would fail...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    but thats not a palindrome obviously
    anyway it would be simple matter to strip spaces before testing.
    mom dad wow dad mom
    thats a palindrome
    momdad wow dad mom
    is not a palindrome. Sure the letters appear in same order but it is not a palindrome it doesnt read same backwars as forwards.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well is "ab cba" a palindrome? I don't think so

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nor i
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I was under the impression that spaces didn't count. I mean how many palindromes are left otherwise?

    Also, palindrome sites exclude spaces.
    ex: http://www.palindromelist.com/
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well at the top of the page they have this....
    Palindrome: A word, phrase, verse, or sentence that reads the same backward or forward.
    and yet they try to tell you that this is a palindrome...
    A dog! A panic in a pagoda!
    which backwards is obviously....
    !adogap a ni cinap a !god A
    really doesnt read same backwards as forwards, but hell if that really what you want as i said previously its incredibly simple to strip spaces and punctuation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Most of these homework palindrome posts don't care about spaces. It's quite common, as anyone who has been around here for any time can tell you. Which is why I made the comment.

    While it may be trivial to ignore whitespace, yours didn't, which again is why I made the comment. So to recap:

    common palindrome problem + code that doesn't account for it = my reply telling you it doesn't fulfill the common palindrome problem

    Do a forum search for palindrome if you're still in doubt.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well you learn something everyday. I really thought a palindrome was of the straw warts variety. And I agree my code will not as it is find palindromes with spaces/punctuation in unless they are true mirror images but even so stripping that punctuation and spaces from the string it will do an admirable job. As its homework left as an exercise for the OP.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Bored again doh!
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    int IsMirrorPalindrome( const char* word, size_t length )
    {
       if( length <= 1) return 1;
       else
       return ( (word[0] == word[length-1]) && IsMirrorPalindrome(word+1,length-2));
    }
    
    int IsPalindrome(std::string test)
    {
       std::string tempcopy;
       std::string::iterator iter = test.begin();
       const std::string::iterator end = test.end();
       while ( iter != end )
       {
          const char c = *iter;
          if ( (!std::ispunct(c)) && (!std::isspace(c)) ) tempcopy.push_back(std::tolower(c));
          ++iter;
       }
    
       if (IsMirrorPalindrome(tempcopy.c_str(),tempcopy.length()))
          return 1;
       else
          return 0;
    }
    
    
    int main()
    {
       std::string test;
       std::cout<<" enter string.... ";
       std::getline(std::cin,test);
       if (IsPalindrome(test))
          std::cout<<std::endl<<"palindrome"<<std::endl;
       else
          std::cout<<std::endl<<" not palindrome"<<std::endl;
       getchar();
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    24
    me too!

    Code:
    bool isPalindrome(const char * data) 
    {
     for(const char * tail = data + strlen(data) - 1; data < tail; tail--, data++) 
     {
      while(isspace(*data) || ispunct(*data)) data++;
      while(isspace(*tail) || ispunct(*tail)) tail--; 
      if(data < tail) 
      {
       if(*data != *tail) return false;
      }  
     }
     return true;  
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM