Question: Application #4 Write a Java program that uses a stack to test whether an input string is a palindrome. A palindrome is a string of

Application #4

Write a Java program that uses a stack to test whether an input string is a palindrome. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward if you ignore spaces, punctuation, and case. Design and implement an algorithm that utilizes one stack and no equals to check this property. HINT: you should push half of the characters on the stack.

Skeleton of PalindromeChecker class is provided.

public class PalindromeChecker { /**  * Tests whether a string is a palindrome, ignoring punctuation, spaces, and case.  *  * @param input a string to check  */  public boolean isPalindrome(String input) { // TODO PROJECT #4   // utilize replaceAll method to remove all punctuation, spaces from the input // utilize toLowerCase method to convert all the input characters to lower case // push half of the characters on the stack // in another loop pop characters from the stack and compare with the remaining characters in the input string // stop the loop as soon as the first unequal pair is found return false; // THIS IS A STUB } // end isPalindrome public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); PalindromeChecker pc = new PalindromeChecker(); //Describe the program and how it works System.out.println("*** This program determines whether a string is a palindrome. "); System.out.println("A palindrome is spelled the same from left to right as it is from right to left,"); System.out.println("if we ignore punctuation, spaces, and case. ***"); System.out.println(" Enter a string that you want to check (or enter \"stop\" to stop): "); String input = keyboard.nextLine(); while (!input.equalsIgnoreCase("stop")) { if (pc.isPalindrome(input)) System.out.println("\"" + input + "\" is a palindrome!"); else System.out.println("\"" + input + "\" is not a palindrome!"); System.out.println(" Enter a string that you want to check (or enter \"stop\" to stop): "); input = keyboard.nextLine(); } // end while System.out.println("Done!"); } // end main } // end PalindromeChecker

SAMPLE RUN

*** This program determines whether a string is a palindrome.

A palindrome is spelled the same from left to right as it is from right to left,

if we ignore punctuation, spaces, and case. ***

Enter a string that you want to check (or enter "stop" to stop):

Race car

---> Checking: "racecar"

"Race car" is a palindrome!

Enter a string that you want to check (or enter "stop" to stop):

A man, a plan, a canal: Panama!

---> Checking: "amanaplanacanalpanama"

"A man, a plan, a canal: Panama!" is a palindrome!

Enter a string that you want to check (or enter "stop" to stop):

abracadabra

---> Checking: "abracadabra"

"abracadabra" is not a palindrome!

Enter a string that you want to check (or enter "stop" to stop):

stop

Done!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!