Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Determines whether a given String is a palindrome.
- *
- * @author Vincent Ong
- * @version 1/30/13
- */
- import java.util.Scanner;
- public class RecursivePalindrome
- {
- public static String helper(String input)
- {
- if(input.equals(""))return input;
- int n = 0;
- String character = input.substring(n, n + 1);
- char first = input.charAt(0);
- if (n == input.length())
- input = input;
- else if (((char)(first) >= 65) && ((char)(first) <= 90))
- {
- input = input.replaceAll(character, character.toLowerCase());
- n++;
- input = helper(input);
- }
- else if((char)(first) == 32 || (char)(first) == 44 || (char)(first) == 46)
- {
- //input = input.replaceAll(character, character.replace("[,. ]", ""));
- input = input.substring(1, input.length());
- n++;
- input = helper(input);
- }
- else
- {
- n++;
- input = input.charAt(0) + helper(input.substring(1, input.length()));
- }
- return input;
- }
- public static boolean isPalindrome(String input)
- {
- String newWord = input.substring(1, input.length() - 1);
- if(newWord.length() <= 1)
- {
- return true;
- }
- return isPalindrome(newWord) && (newWord.charAt(0) == (newWord.charAt(newWord.length() - 1)));
- }
- public static void main(String[] args)
- {
- System.out.println(isPalindrome("peep"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment