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

1. Palindrome Detector (30 pts) : MUST BE IN JAVA...A palindrome is a phrase tha

ID: 3669503 • Letter: 1

Question

1.     Palindrome Detector (30 pts) : MUST BE IN JAVA...A palindrome is a phrase that reads the same forwards as it does backwards. For example, "a man, a plan, canal, Panama." is a palindrome. Ignore white space and punctuation. Write a program that uses a stack to check for palindromes in each line of a text file. The main method should be in a class called FindPalindromes.java.The text file should be provided as a command line argument (remember String[] args from main). Try your program on the example text filepalindromes.txt. Your program should output the palindromes that it finds in the document. For example:

java FindPalindromes palindromes.txt

     "a man, a plan, a canal, Panama" is a palindrome.

     "Don't nod" is a palindrome.

     "Taco Cat!" is a palindrome.

You must write your own MyStack class for this problem. It must be generic, in the sense that you should be able to use this stack with any type of object. Don't use the built in Stack, instead build your stack from java.util.LinkedList. Do not use the builtin push/pop methods from LinkedList.

Explanation / Answer

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.Scanner;
import java.io.*;
public class Palindrome
{
public static void main(String[] args) throws IOException
{
BufferedReader in=new BufferedReader(new FileReader("Palindrome.txt"));
PrintStream out=new PrintStream(new File("****.txt"));
boolean palin;
String input,reveresed;
input=in.readLine();
while(input!=null)
{
if (isPalindrome(input))
out.println(input+"yoo This is a Palindrome");
else
out.println(input+"Huff this is not a Palindrome");
input=in.readLine();
}
}
public static boolean isPalindrome(String input)
{
Queue<Character> q = new LinkedList<Character>( );
Stack<Character> s = new Stack<Character>( );
Character letter;
int mismatches = 0;
int i;
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i);
if (Character.isLetter(letter))
{
q.add(letter);
s.push(letter);
}
}
while (!q.isEmpty( ))
{
if (q.remove( ) != s.pop( ))
mismatches++;
}
return (mismatches == 0);
}
}