Advertisement
Vermiculus

Palindrome Testing

Apr 12th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Vermiculus
  4.  * @version 0.0
  5.  *
  6.  */
  7. public class meep {
  8.  
  9.     /**
  10.      * Entry point for the program
  11.      *
  12.      * @param args
  13.      *            - command line arguments
  14.      * @since The beginning of TIME ITSELF!!!
  15.      *
  16.      */
  17.     public static void main(String[] args) {
  18.         System.out.print("Please input a string: ");
  19.         String t = (new java.util.Scanner(System.in)).next();
  20.         System.out.printf("\"%s\" is %s palindrome", t,
  21.                 isPalindrome(t) ? "a"
  22.                         : "not a");
  23.     }
  24.  
  25.     /**
  26.      *
  27.      * @param testString
  28.      *            - the String to test for palindrome
  29.      * @return a boolean representing whether the string is a weak palindrome (/spaces, /case)
  30.      */
  31.     public static boolean isPalindrome(String testString) {
  32.         // iterate through String to remove spaces
  33.         // test that the first and last characters are equal
  34.         // recursive with the inner string
  35.         try {
  36.             testString = removeSpaces(testString);
  37.             testString = testString.toLowerCase();
  38.             if (testString.charAt(0) == testString
  39.                     .charAt(testString.length() - 1)) {
  40.                 if (testString.length() == 0 || testString.length() == 1)
  41.                     return true;
  42.                 return isPalindrome(testString.substring(1,
  43.                         testString.length() - 1));
  44.             } else
  45.                 return false;
  46.         } catch (StringIndexOutOfBoundsException t) {
  47.             return true;
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Removes spaces from a string
  53.      *
  54.      * @param s
  55.      *            the string to remove spaces from
  56.      * @param i
  57.      *            the index to start at
  58.      * @return the string without any spaces
  59.      */
  60.     public static String removeSpaces(String s, int i) {
  61.         if (i == s.length() - 1) {
  62.             return s;
  63.         } else if (s.charAt(i) == ' ') {
  64.             s = removeCharAt(s, i);
  65.             return removeSpaces(s, i + 1);
  66.         }
  67.         return removeSpaces(s, i + 1);
  68.     }
  69.  
  70.     /**
  71.      * Removes spaces from a string
  72.      *
  73.      * @return the string without any spaces
  74.      */
  75.     public static String removeSpaces(String s) {
  76.         if (!s.contains(" "))
  77.             return s;
  78.         return removeSpaces(s, 0);
  79.     }
  80.  
  81.     public static String removeCharAt(String s, int i) {
  82.         String a = s.substring(0, i);
  83.         String b = s.substring(i + 1);
  84.         return a + b;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement