Created
March 10, 2014 21:38
-
-
Save frogbandit/9474940 to your computer and use it in GitHub Desktop.
Find Palindromes: Identifies and prints out palindromes from a text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//James Xue | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
public class FindPalindromes | |
{ | |
public static void main(String args[]) throws IOException { | |
//create empty stack, get input | |
MyStack<Character> stack = new MyStack<Character>(); | |
File text = new File(args[0]); | |
BufferedReader reader = new BufferedReader(new FileReader(text)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
//remove capitalization, whitespace, and punctuation from input | |
String letters = line.toLowerCase().replaceAll("[\\W]", ""); | |
//push each character from input onto empty stack | |
for (int i =0; i < letters.length(); i++) { | |
stack.push(letters.charAt(i)); | |
} | |
//empty String | |
String reverse = ""; | |
//pop each character onto empty String | |
while (!stack.isEmpty()) | |
reverse += stack.pop(); | |
//compare two Strings. If equal, they are palindromes. | |
if (letters.equals(reverse)) | |
System.out.println('"' + line + '"' + " is a palindrome."); | |
} | |
reader.close(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//James Xue | |
//MyStack class implemented using ArrayLists | |
import java.util.ArrayList; | |
public class MyStack<E> | |
{ | |
private ArrayList<E> stack; | |
public MyStack() | |
{ | |
stack = new ArrayList<E>(); | |
} | |
//push method adds element to ArrayList | |
public void push(E item) | |
{ | |
stack.add(item); | |
} | |
//pop first object off of stack (last object of ArrayList) | |
public Object pop() | |
{ | |
if (isEmpty() == false) | |
return stack.remove(stack.size() - 1); | |
else | |
return null; | |
} | |
//returns if stack is empty or not | |
public boolean isEmpty() | |
{ | |
if (stack.size() == 0) | |
return true; | |
else | |
return false; | |
} | |
//converts to string | |
public String show() | |
{ | |
if (isEmpty() == false) | |
return stack.toString(); | |
else | |
return null; | |
} | |
//returns stack size | |
public int getSize() | |
{ | |
return stack.size(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Are the following lines palindromes? | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment