Thread: palindrome recursion

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    palindrome recursion

    I am asked to write the following, which for me seems to be pretty stupid

    Write a recursive function that takes as an input two strings s1, s2 and checks whether one is a palindrome of the other

    If s1 and s2 are palindrome of each other then it means that they are equal right? I mean s1 and s2 are equal? So why do I need to write a recursion for this, just use a strcmp of both strings and it should be done. Or am I misunderstanding the question

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    apparently "never even" is a valid palindrome (according to Wikipedia)

    so just comparing the two strings isn't enough
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    ok, so how do I write this function then? is the base case when we're only comparing a char and if both char are equal then we return 1, indicating it's a palindrome otherwise we just do a substring of the string...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is a palindrome? You need to get that right first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by laserlight View Post
    What is a palindrome? You need to get that right first.
    Palindrome - Wikipedia, the free encyclopedia
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ಠ_ಠ View Post
    Yes, that article makes the same point: there are multiple definitions. If your definition does not match your instructor's definition, then your solution will be wrong. In any case, your currently used definition (i.e., "string equality") does not match any definition of "palindrome" that I know of.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by -EquinoX- View Post
    ok, so how do I write this function then? is the base case when we're only comparing a char and if both char are equal then we return 1, indicating it's a palindrome otherwise we just do a substring of the string...
    I think you could concatenate the two strings and compare the start and end(skipping the '\0' char at the very end) of the new string, you might need to remove any spaces depending on your input
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ಠ_ಠ
    I think you could concatenate the two strings and compare the start and end(skipping the '\0' char at the very end) of the new string, you might need to remove any spaces depending on your input
    I do not think that string concatenation is needed even for a recursive solution. For the string to be compared in the forward direction, just some pointer arithmetic should suffice. For the string to be compared in the reverse direction, (assuming the requirements permit it) some index could be used and thus it would be the index that is changed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by laserlight View Post
    I do not think that string concatenation is needed even for a recursive solution. For the string to be compared in the forward direction, just some pointer arithmetic should suffice. For the string to be compared in the reverse direction, (assuming the requirements permit it) some index could be used and thus it would be the index that is changed.
    would that allow for different sized strings?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ಠ_ಠ
    would that allow for different sized strings?
    Yes, assuming an index is used for the backward direction. If not, it might be a little difficult since there is no way of knowing when you have reached to before the front of the string (and hence are with with an "empty string", recursively speaking).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  3. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  4. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM
  5. Recursion function won't exit
    By cheesehead in forum C++ Programming
    Replies: 20
    Last Post: 10-30-2001, 03:58 PM