Answer:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
test("A Santa at NASA");
test("My gym");
test("taco cat");
test("kayak");
test("NOW I WON");
test("race car");
test("bookkeeper");
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.print("Enter a word or phrase: ");
String line = scanner.nextLine();
if (line.equals("exit")) break;
if (Palindromes.isPalindrome(line)) {
System.out.println("PALINDROME");
} else {
System.out.println("NOT PALINDROME");
}
}
scanner.close();
}
public static void test(String s) {
System.out.printf("'%s' is %sa palindrome\\", s, Palindromes.isPalindrome(s) ? "" : "not ");
}
}
class Palindromes {
static boolean isPalindrome(String s) {
s = s.replaceAll("\\s","").toLowerCase();
return s.equals(reverseString(s));
}
public static String reverseString(String str){
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString();
}
}
Step-by-step explanation:
I chose to use a string reverse routine. This is slightly less efficient than iterating the string from both ends, but performance is less important than readability here.
The stop word is exit.