Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to check if a piece of text that the user enters is a palindrome

ID: 3722195 • Letter: W

Question

Write a program to check if a piece of text that the user enters is a palindrome. In this case, you add each character to both a stack and a queue as it is read.

After each character has been put onto both the stack and the queue, start removing characters from both the stack and the queue, and comparing them. If the word is a palindrome, the first character popped will be the same as the first character dequeued, and so on for the second and subsequent characters until the stack and queue are empty.

You MUST follow the approach described here to test for palindromes; any other approach not using stacks and queues, will earn you 0 marks.

Palindrome comparisons are normally not case-sensitive, so you could convert all characters to uppercase first. Also, non-alphanumeric characters, such as punctuation and spaces, are usually ignored. For maximum marks, figure out how to use the appropriate method in Java’s Character class to do this.

Stack Class

ArrayStack Class

Queue Class

ArrayQueue Class

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.Scanner;

public class CheckPalindrome {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String str;
System.out.println("Enter some text: ");
str = keyboard.nextLine();

ArrayStack stack = new ArrayStack();
ArrayQueue queue = new ArrayQueue();

//convert to upper case
str = str.toUpperCase();
char ch ;
for(int i = 0; i < str.length(); i++)
{
ch = str.charAt(i);
if(Character.isAlphabetic(ch))
{
stack.push(ch);
queue.enqueue(ch);
}
}


boolean palindrome = true;
while(!stack.isEmpty())
{
if(!stack.pop().equals(queue.dequeue()))
{
palindrome = false;
break;
}
}

if(palindrome)
System.out.println("The given text is a palindrome");
else
System.out.println("The given text is NOT a palindrome");
}

}


output
======
Enter some text:
Race Car!
The given text is a palindrome


Enter some text:
hello there!
The given text is NOT a palindrome

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote