By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,105 Members | 2,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,105 IT Pros & Developers. It's quick & easy.

output the longest palindrome

P: 2
Write a Java program that prints the longest palindrome in an input file to standard output. A palindrome is a string whose reverse is the same as the original. Ignore case, whitespace, and punctuation in the input. If there are more than one palindrome of the maximum length, print the first one. Remove all space and punctuation and convert upper case to lower case in the output. If the input file contains:

Was it a rat I saw?

Your program prints:

% java Palindrome < inputfile
wasitaratisaw
Sep 14 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Write a Java program that prints the longest palindrome in an input file to standard output. A palindrome is a string whose reverse is the same as the original. Ignore case, whitespace, and punctuation in the input. If there are more than one palindrome of the maximum length, print the first one. Remove all space and punctuation and convert upper case to lower case in the output. If the input file contains:

Was it a rat I saw?

Your program prints:

% java Palindrome < inputfile
wasitaratisaw
Should the program print the same if it were feeded;

Foo Was it a rat I saw bar?

kind regards,

Jos
Sep 14 '07 #2

P: 2
Should the program print the same if it were feeded;

Foo Was it a rat I saw bar?

kind regards,

Jos
yes, it will still print " wasitaratisaw".
Sep 14 '07 #3

10K+
P: 13,264
Have you worked out your algorithm for it yet?
Sep 15 '07 #4

Expert 10K+
P: 11,448
An utterly naive algorithm would take O(n^2) steps: it checks all indexes j > i+1
whether substring s_i ... s_j is a palindrome or not. Cleverly in/decrementing
both index values would find larger palindromes first and cut off the search for
smaller palindromes; worst case it would still be a O(n^2) algorithm.

kind regards,

Jos
Sep 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.