I have the program here. All i have to is Revise this program such that all char
ID: 3642123 • Letter: I
Question
I have the program here. All i have to is Revise this program such that all characters other than lettersand digits are ignored in the process of deciding whether or
not a string is a palindrome. So, the following strings, for
example, should be considered palindromes.
Race car
A man, a plan, a canal, Panama!
HINT: The built-in Java method Character.isLetterOrDigit(c)
returns true if the value of the char variable c is a letter or a
digit, and false, otherwise.
PROGRAM:
//////////////////////////////////////////////////////////
//
// This program determines whether a given string
// is a palindrome. Differences in case (upper
// vs. lower) are ignored.
//
// Author: jam
//
//////////////////////////////////////////////////////////
import java.util.*;
public class pal
{
public static void main(String[] args)
{
String aString;
char cur_char, tos, foq;
Stack<Character> the_stack;
Queue<Character> the_queue;
boolean is_pal;
int i;
Scanner sc = new Scanner(System.in);
the_stack = new Stack<Character>();
the_queue = new ArrayDeque<Character>();
System.out.print("Enter a string: ");
aString = sc.nextLine();
// normalize the string, that is, make it all uppercase
aString = aString.toUpperCase();
// normalize the string, that is, make it all uppercase
aString = aString.toUpperCase();
// Push and enqueue the chars of the string
for(i = 0; i < aString.length(); i++)
{
cur_char = aString.charAt(i);
the_stack.push(cur_char);
the_queue.add(cur_char);
}
// Now pop and dequeue to determine if the string is a palindrome
is_pal = true;
while(!the_queue.isEmpty() && is_pal)
{
tos = the_stack.pop();
foq = the_queue.remove();
// If there's a discrepancy between the TOS and the front
// of the queue, then the string is *not* a palindrome.
// We can stop the processing loop
if(tos != foq)
is_pal = false;
}
System.out.println();
if( is_pal )
System.out.println("The string is a palindrome.");
else
System.out.println("The string is NOT a palindrome.");
} // end main
} // end pal
THANK YOU>>>OUTPUT'll BE MUCH APPRECIATED>>>>>
Explanation / Answer
import java.util.*;
public class pal {
public static void main(String[] args) {
String aString;
char cur_char, tos, foq;
Stack<Character> the_stack;
Queue<Character> the_queue;
boolean is_pal;
int i;
Scanner sc = new Scanner(System.in);
the_stack = new Stack<Character>();
the_queue = new ArrayDeque<Character>();
System.out.print("Enter a string: ");
aString = sc.nextLine();
// normalize the string, that is, make it all uppercase
aString = aString.toUpperCase();
// Push and enqueue the chars of the string
for(i = 0; i < aString.length(); i++) {
cur_char = aString.charAt(i);
if(Character.isLetterOrDigit(cur_char)) {
the_stack.push(cur_char);
the_queue.add(cur_char);
}
}
// Now pop and dequeue to determine if the string is a palindrome
is_pal = true;
while(!the_queue.isEmpty() && is_pal) {
tos = the_stack.pop();
foq = the_queue.remove();
// If there's a discrepancy between the TOS and the front
// of the queue, then the string is *not* a palindrome.
// We can stop the processing loop
if(tos != foq)
is_pal = false;
}
System.out.println();
if( is_pal )
System.out.println("The string is a palindrome.");
else
System.out.println("The string is NOT a palindrome.");
} // end main
} // end pal