ThatSnail

Untitled

Feb 2nd, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1.  
  2. /**
  3. * Determines whether a given String is a palindrome.
  4. *
  5. * @author Vincent Ong
  6. * @version 1/30/13
  7. */
  8. import java.util.Scanner;
  9. public class RecursivePalindrome
  10. {
  11. public static String helper(String input)
  12. {
  13. if(input.equals(""))return input;
  14. int n = 0;
  15. String character = input.substring(n, n + 1);
  16. char first = input.charAt(0);
  17.  
  18. if (n == input.length())
  19. input = input;
  20. else if (((char)(first) >= 65) && ((char)(first) <= 90))
  21. {
  22. input = input.replaceAll(character, character.toLowerCase());
  23. n++;
  24. input = helper(input);
  25. }
  26. else if((char)(first) == 32 || (char)(first) == 44 || (char)(first) == 46)
  27. {
  28. //input = input.replaceAll(character, character.replace("[,. ]", ""));
  29. input = input.substring(1, input.length());
  30. n++;
  31. input = helper(input);
  32. }
  33. else
  34. {
  35. n++;
  36. input = input.charAt(0) + helper(input.substring(1, input.length()));
  37. }
  38.  
  39. return input;
  40. }
  41.  
  42. public static boolean isPalindrome(String input)
  43. {
  44. String newWord = input.substring(1, input.length() - 1);
  45. if(newWord.length() <= 1)
  46. {
  47. return true;
  48. }
  49. return isPalindrome(newWord) && (newWord.charAt(0) == (newWord.charAt(newWord.length() - 1)));
  50. }
  51. public static void main(String[] args)
  52. {
  53. System.out.println(isPalindrome("peep"));
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment