COSC 3331-Data Structures and Algorithms A palindrome is a phrase that reads the
ID: 673414 • Letter: C
Question
COSC 3331-Data Structures and Algorithms
A palindrome is a phrase that reads the same forwards as it does backwards. For example, “a man, a plan, a canal, Panama,” is a palindrome. Write a program that uses a stack data structure to check for palindromes in each line of a text file. The stackX class is from LISTING 4.2. Test your program on the following example text file,
A man, a plan, a canal, Panama. This line is not a palindrome Don't nod The next one might be my favorite Taco Cat! Another non-palindrome Rats live on no evil star. If your program finds this line, it's not working Neil, a trap! Sid is part alien! Step on no pets. Dammit, I'm mad! Madam, I'm Adam. Madam, in Eden, I'm Adam. Rise to vote, sir. Never odd or even If I had a hi-fi Yo, banana boy! Do geese see God? No devil lived on. Ah, Satan sees Natasha. Lewd did I live & evil I did dwel! A dog, a panic in a pagoda Was it a cat I saw? Was it a car or a cat I saw? A Toyota's a Toyota. Another non-palindrome No lemons, no melon Now I see bees, I won. Ma is as selfless as I am. Nurse, I spy gypsies—run! The next one isn't as cool as the Panama one A dog, a plan, a canal, pagoda Was it Eliot's toilet I saw? Some of these are hilarious. Papaya war?! No, sir, away! A papaya war is on! Go hang a salami, I'm a lasagna hog. I, madam, I made radio! So I dared! Am I mad? Am I? Swap God for a janitor, rot in a jar of dog paws. Eva, can I see bees in a cave? Not a palindrome So many dynamos! Red rum, sir, is murder.
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("palsout.txt"));
boolean palin;
String input,reveresed;
input=in.readLine();
while(input!=null)
{
if (isPalindrome(input))
out.println(input+" is a Palindrome");
else
out.println(input+" is not a Palindrome");
input=in.readLine();
}}
/**
* The return value is true if and only if the input string is a palindrome.
* All non-letters is ignored, and the case of the letters is also ignored.
**/
public static boolean isPalindrome(String input)
{
Queue<Character> q = new LinkedList<Character>( );
Stack<Character> s = new Stack<Character>( );
Character letter; // One character from the input string
int mismatches = 0; // Number of spots that mismatched
int i; // Index for the input string
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++;
}
// If there were no mismatches, then the string was a palindrome.
return (mismatches == 0);
}
}
YOU CAN ALSO TRY THIS CODE
import java.util.LinkedList;
02
import java.util.Queue;
03
import java.util.Stack;
04
import java.util.Scanner;
05
import java.io.*;
06
public class Palindrome
07
{
08
public static void main(String[] args) throws IOException
09
{
10
BufferedReader in=new BufferedReader(new FileReader("Palindrome.txt"));
11
PrintStream out=new PrintStream(new File("palsout.txt"));
12
boolean palin;
13
String input,reveresed;
14
input=in.readLine();
15
while(input!=null)
16
{
17
if (isPalindrome(input))
18
out.println(input+" is a Palindrome");
19
else
20
out.println(input+" is not a Palindrome");
21
input=in.readLine();
22
}}
23
/**
24
* The return value is true if and only if the input string is a palindrome.
25
* All non-letters is ignored, and the case of the letters is also ignored.
26
**/
27
public static boolean isPalindrome(String input)
28
{
29
Queue<Character> q = new LinkedList<Character>( );
30
Stack<Character> s = new Stack<Character>( );
31
Character letter; // One character from the input string
32
int mismatches = 0; // Number of spots that mismatched
33
int i; // Index for the input string
34
for (i = 0; i < input.length( ); i++)
35
{
36
letter = input.charAt(i);
37
if (Character.isLetter(letter))
38
{
39
q.add(letter);
40
s.push(letter);
41
}
42
}
43
while (!q.isEmpty( ))
44
{
45
if (q.remove( ) != s.pop( ))
46
mismatches++;
47
}
48
// If there were no mismatches, then the string was a palindrome.
49
return (mismatches == 0);
50
}
51
}
import java.util.LinkedList;
02
import java.util.Queue;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.