class PalindromeTester
{
public static void main(String[]args)
{
String word, run = "y";
Scanner scan = new Scanner(System.in);
while ((run.substring(0, 1)).equals("y")) //Program keeps running while the input starts with 'y' and halts if it starts with anything else
{
System.out.println();
System.out.print("Enter a potential palindrome: ");
word = scan.nextLine();
System.out.println(); //scans the inputted palindrome then assigns it to a variable and passes it to the paltest() method
paltest(word);
System.out.println();
System.out.print("Test another palindrome (y/n)? ");
run = scan.nextLine();
while (run.length() == 0) //Error checking in case no input was entered for 'run' in the outer loop, prevents StringIndexOutOfBounds
{
System.out.println();
System.out.println("*Please enter yes or no*");
System.out.println();
System.out.print("Test another palindrome (y/n)? ");
run = scan.nextLine();
}
run = run.toLowerCase(); //sets the input for 'run' to lower case so the program will keep running on input of 'y' regardless of case
}
}
public static void paltest(String word)
{
int left, right;
word = word.toLowerCase(); //sets the argument passed from main method to lower case so case is ignored in evaluation and assigns it to local variable 'word'
word = word.replaceAll(",", ""); //deletes all instances of punctuation and spaces in the string
word = word.replaceAll("[.]", "");
word = word.replaceAll("\'", "");
word = word.replaceAll(";", "");
word = word.replaceAll("\"", "");
word = word.replaceAll("\\(", "");
word = word.replaceAll("\\)", "");
word = word.replaceAll("\\[", "");
word = word.replaceAll("\\]", "");
word = word.replaceAll("\\{", "");
word = word.replaceAll("\\}", "");
word = word.replaceAll(" ", "");
left = 0;
right = word.length();
if (word.length() <= 1) //Checks to see if the word is long enough to be a palindrome and whether to evaluate the string or not
{
System.out.println("Please enter a word that is two or more characters long");
}
else if (word.length() <= 3) //base case, if the word length is at least 3, then it can be considered for palindrome checking
{
if (word.charAt(left) == word.charAt(right-1)) //Checks the first and last characters of the string if they're equal,
System.out.println("-This IS a palindrome-"); //it is a palindrome if they are, the middle character is irrelevant in a string of 3 characters
else
System.out.println("-This is NOT a palindrome-");
}
else
{ //if the word is longer than 3 characters, check the first and last character to see if its equal
if (word.charAt(left) == word.charAt(right-1))
{ //if it is equal, cut off the first and last characters of the string and call the paltest() method again with the new shorter string
left += 1;
right -= 1;
word = word.substring(left, right);
paltest(word); //keep calling paltest() with a new string with the first and last characters removed until it reaches the base case or until
} //the first and last characters are not equal
else //if the word is longer than 3 characters and the first and last characters are not equal
{
System.out.println("-This is NOT a palindrome-");
}
}
}
}