Advertisement
TheBat

isPalindrome

Apr 4th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /**
  2. *
  3. * Test a string to see if it is a palindrome.
  4. *
  5. * @param str
  6. * A string to be tested to see if it is a palindrome
  7. * @return true if str is a palindrome, false otherwise
  8. *
  9. *
  10. */
  11. public static boolean isPalindrome(String str) {
  12. if(str.length() == 0) return true;
  13. else if(str.length() % 2 != 0)return isPalindromeO(str, 0, str.length()-1);
  14. else return isPalindromeE(str, 0, str.length()-1);
  15. }
  16.  
  17. /**
  18. * Recursive method to test if a string is a palindrome.
  19. *
  20. * @return true if str is a palindrome, false otherwise
  21. *
  22. * Add parameters however you see fit.
  23. */
  24. private static boolean isPalindromeO(String str, int a, int b) {
  25. if(str.charAt(a) != str.charAt(b))return false;
  26. if(a == b && str.charAt(a) == str.charAt(b))return true;
  27. else return isPalindromeO(str, ++a, --b);
  28. }
  29. /**
  30. * Recursive method to test if a string is a palindrome.
  31. *
  32. * @return true if str is a palindrome, false otherwise
  33. *
  34. * Add parameters however you see fit.
  35. */
  36. private static boolean isPalindromeE(String str, int a, int b) {
  37. if(a > b)return true;
  38. if(str.charAt(a) != str.charAt(b))return false;
  39. return isPalindromeE(str, ++a, --b);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement