Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Palindrome check

  1. Jun 29, 2004 #1
    Would you tell me how to write a test_function to check a given string whether it is a palindrome or not in Java recursively ?
    Thank you,

    -Pattielli
     
  2. jcsd
  3. Jun 29, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The initial condition of the recursion is that the function is checking the first and last characters to see if they're the same. In other words, the recursive function is called with two arguments: (0, string.length() - 1). I will call those arguments a and b.

    The function checks the two characters at the positions (a and b) given by the arguments. If they are not the same, the function return false.

    If they are the same, the function first checks to see if it has met the condition to complete the check. If a and b are the same number, the recursion has reached the center of an odd-length palindrome. If b is one greater than a, then the recursion has reached the center of an even-length palindrome. If either of these conditions is true, the function returns true. If these conditions are not true, the check is not complete yet, and the function calls itself with arguments (a+1, b-1), and returns whatever result that call returns.

    - Warren
     
  4. Jun 30, 2004 #3
    Many Thanks to Warren, I understand and made my program work fine as what you told me, Thanks a lot,

    - Pattielli
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Palindrome check
  1. Check others IP? (Replies: 14)

  2. Cryptanalysis Check (Replies: 1)

Loading...