palindrome recursion

This is a discussion on palindrome recursion within the C Programming forums, part of the General Programming Boards category; I am asked to write the following, which for me seems to be pretty stupid Write a recursive function that ...

  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
    21,725
    What is a palindrome? You need to get that right first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,725
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,725
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,725
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:45 PM
  2. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 11:36 AM
  3. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:05 PM
  4. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM
  5. Recursion function won't exit
    By cheesehead in forum C++ Programming
    Replies: 20
    Last Post: 10-30-2001, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21