Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Vermiculus
- * @version 0.0
- *
- */
- public class meep {
- /**
- * Entry point for the program
- *
- * @param args
- * - command line arguments
- * @since The beginning of TIME ITSELF!!!
- *
- */
- public static void main(String[] args) {
- System.out.print("Please input a string: ");
- String t = (new java.util.Scanner(System.in)).next();
- System.out.printf("\"%s\" is %s palindrome", t,
- isPalindrome(t) ? "a"
- : "not a");
- }
- /**
- *
- * @param testString
- * - the String to test for palindrome
- * @return a boolean representing whether the string is a weak palindrome (/spaces, /case)
- */
- public static boolean isPalindrome(String testString) {
- // iterate through String to remove spaces
- // test that the first and last characters are equal
- // recursive with the inner string
- try {
- testString = removeSpaces(testString);
- testString = testString.toLowerCase();
- if (testString.charAt(0) == testString
- .charAt(testString.length() - 1)) {
- if (testString.length() == 0 || testString.length() == 1)
- return true;
- return isPalindrome(testString.substring(1,
- testString.length() - 1));
- } else
- return false;
- } catch (StringIndexOutOfBoundsException t) {
- return true;
- }
- }
- /**
- * Removes spaces from a string
- *
- * @param s
- * the string to remove spaces from
- * @param i
- * the index to start at
- * @return the string without any spaces
- */
- public static String removeSpaces(String s, int i) {
- if (i == s.length() - 1) {
- return s;
- } else if (s.charAt(i) == ' ') {
- s = removeCharAt(s, i);
- return removeSpaces(s, i + 1);
- }
- return removeSpaces(s, i + 1);
- }
- /**
- * Removes spaces from a string
- *
- * @return the string without any spaces
- */
- public static String removeSpaces(String s) {
- if (!s.contains(" "))
- return s;
- return removeSpaces(s, 0);
- }
- public static String removeCharAt(String s, int i) {
- String a = s.substring(0, i);
- String b = s.substring(i + 1);
- return a + b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement