JAVA Program modify this program below: so it does not use a while loop. Instead it uses recursive method that doesn't copy the string over and over again. import java.util.Scanner; public class PalindromeChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.print("Please enter a string to test for palindrome or type QUIT to exit:\n"); String input = scanner.nextLine().toLowerCase(); if (input.equals("quit")) { break; } if (isPalindrome(input)) { System.out.println("The input is a palindrome."); } else { System.out.println("The input is not a palindrome."); } } } private static boolean isPalindrome(String str) { str = str.replaceAll("[^a-zA-Z0-9]", ""); int left = 0; int right = str.length() - 1; while (left < right) { if (str.charAt(left) != str.charAt(right)) { return false; } left++; right--; } return true; } } Chapter 16. PC #5. Palindrome Detector (page 1073) A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that uses recursion to determine whether a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. The program should ask the user to enter a string, which is checked for palindrome property. The program displays whether the given input is a palindrome or not, then prompts the user to enter another string. If the user enters QUIT (case insensitive, then exit the program). Test Case 1 Please enter a string to test for palindrome or type QUIT to exit:\n Desserts, I stressedENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n KayakENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 2 Please enter a string to test for palindrome or type QUIT to exit:\n dadENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 3 Please enter a string to test for palindrome or type QUIT to exit:\n momENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n QuitENTER Test Case 4 Please enter a string to test for palindrome or type QUIT to exit:\n helloENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quiTENTER Please enter a string to test for palindrome or type QUIT to exit:\n b,a,dENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 6 Please enter a string to test for palindrome or type QUIT to exit:\n Able was I, ere I saw ElbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 7 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n A man, a plan, a canal, PanamaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 8 Please enter a string to test for palindrome or type QUIT to exit:\n @abENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n ab@ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n @aaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aa@ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 9 Please enter a string to test for palindrome or type QUIT to exit:\n abbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abcaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aabcaaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abaceedabaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Please enter a string to test for palindrome or type QUIT to exit:\n");
String input = scanner.nextLine().toLowerCase();
if (input.equals("quit")) {
break;
}
if (isPalindrome(input)) {
System.out.println("The input is a palindrome.");
} else {
System.out.println("The input is not a palindrome.");
}
}
}
private static boolean isPalindrome(String str) {
str = str.replaceAll("[^a-zA-Z0-9]", "");
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
Test Case 1
Desserts, I stressedENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
KayakENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 2
dadENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 3
momENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
QuitENTER
Test Case 4
helloENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quiTENTER
b,a,dENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 6
Able was I, ere I saw ElbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 7
A man, a plan, a canal, PanamaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 8
@abENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
ab@ENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
@aaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aa@ENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 9
abbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abcaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aabcaaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abaceedabaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Step by step
Solved in 3 steps with 9 images