Palindrome
-
A palindrome is a string that reads the same in both directions.
For example, the word racecar is a palindrome
but alabama is not (reverse amabala).
To make it more fun, we define a palindrome to be case and punctuation insensitive.
That is, the string Race car! is also considered a palindrome.
Here comes a long one:
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal--Panama!
(discovered by Guy Jacobson), and an even longer one:
A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale,
macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe,
pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal--Panama!
(discovered by Guy Steele).
As the length grows, it is difficult to check if a string is indeed
a palindrome.
In this exercise, we are going to implement a Java program to do this job.
Download skeleton code and follow instructions
here to build a Java project.
Two files are contained in the package:
PalindromeDetector.java and
PalindromeDetectorDriver.java.
The driver program PalindromeDetectorDriver handles
file I/O: it iteratively reads a line from an input file and writes it to the output file
only if the line is detected as a palindrome.
You need to implement the isPalindrome method of
PalindromeDetector,
which takes a string as input and returns true if the string is a palindrome
and false otherwise.
A string is defined as a palindrome if, after removing all non-letter characters
(characters other than a-z and A-Z) and converting all letters to lower case,
it reads identical in both directions.
- Example
-
Input file:
This line, and the two lines below, shouldn't appear in output!
Below is a list of palindromes from
http://en.wikipedia.org/wiki/Palindrome
racecar
Dammit, I'm mad!
If I had a hi-fi
Madam, I'm Adam
Madam, in Eden, I'm Adam
War, sir, is raw
Yo, banana boy!
Do geese see God?
Rise to vote, sir
Was it a cat I saw?
Was it a car or a cat I saw?
Never odd or even
No devil lived on
A Toyota's a Toyota
No lemons, no melon
Now I see bees, I won
Race fast, safe car
Ah, Satan sees Natasha
A dog, a panic in a pagoda
Ma is as selfless as I am
Nurse, I spy gypsies-run!
A man, a plan, a canal: Panama
A dog, a plan, a canal, pagoda
Was it Eliot's toilet I saw?
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?
Taco Cat
Swap God for a janitor, rot in a jar of dog paws.
Mr. Owl ate my metal worm.
Rats live on no evil star.
Eva, can I see bees in a cave?
Step on no pets.
Output file:
racecar
Dammit, I'm mad!
If I had a hi-fi
Madam, I'm Adam
Madam, in Eden, I'm Adam
War, sir, is raw
Yo, banana boy!
Do geese see God?
Rise to vote, sir
Was it a cat I saw?
Was it a car or a cat I saw?
Never odd or even
No devil lived on
A Toyota's a Toyota
No lemons, no melon
Now I see bees, I won
Race fast, safe car
Ah, Satan sees Natasha
A dog, a panic in a pagoda
Ma is as selfless as I am
Nurse, I spy gypsies-run!
A man, a plan, a canal: Panama
A dog, a plan, a canal, pagoda
Was it Eliot's toilet I saw?
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?
Taco Cat
Swap God for a janitor, rot in a jar of dog paws.
Mr. Owl ate my metal worm.
Rats live on no evil star.
Eva, can I see bees in a cave?
Step on no pets.
Sample solution
|