Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > palindrome

Reply
Thread Tools

palindrome

 
 
Benjamin Rushing
Guest
Posts: n/a
 
      04-01-2008
I've been tasked with writing a simple java program where the user inputs a
5 digit number and the program states whether it's a palindrome or not.
I can't use an array or use the 'reverse' function of code. Anybody willing
to give me a hint on how to procede?


 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      04-01-2008
On Tue, 01 Apr 2008 16:16:49 -0700, Benjamin Rushing
<(E-Mail Removed)> wrote:

> I've been tasked with writing a simple java program where the user
> inputs a
> 5 digit number and the program states whether it's a palindrome or not.


Tasked by whom? A teacher assigning homework?

> I can't use an array or use the 'reverse' function of code.


Does the person assigning the task consider a string to be an array? How
about other collection types? Do they mean that you're only prohibited
from using things that are literally arrays? Or is any collection of data
that uses array-like semantics a problem for some reason?

> Anybody willing to give me a hint on how to procede?


It seems to me that the easiest way is to take the input data, and
iterative compare pairs of characters from the beginning and ending of the
string. If you reach the middle of the string and every character
matched, then it's a palindrome. If you find two characters that don't
match, it's not.

As long as you are allowed to use a string in your processing, the above
should be simple enough. Between the String class, a for() loop, and an
if() that controls the flow for each character comparison, you've got
everything you need.

Pete
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-02-2008
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

> Anybody willing
>to give me a hint on how to procede?


you will use a loop that will chase left to right and compute the
corresponding right to left char.

Draw some on paper, writing down the indexes of the characters.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-02-2008
Benjamin Rushing wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.
> I can't use an array or use the 'reverse' function of code. Anybody willing
> to give me a hint on how to procede?


class DetectPalindrome {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
System.out.print(number + " is ");
if (number < 0)
System.out.println("not a palindrome.");
else
System.out.println("a palindrome "
+ " when written in base "
+ Math.max(number + 1L, 2));
}
}

There are, I suppose, less elegant approaches involving
the % operator, or conversion to String form and comparing
the results of carefully-chosen charAt() methods. Ho-hum.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Logan Shaw
Guest
Posts: n/a
 
      04-02-2008
Benjamin Rushing wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.

^^^^^^^^^^^^^^

Guaranteed to be exactly 5 digits? Use this to your advantage!

- Logan
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-02-2008
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>I've been tasked with writing a simple java program where the user inputs a
>5 digit number and the program states whether it's a palindrome or not.
>I can't use an array or use the 'reverse' function of code. Anybody willing
>to give me a hint on how to procede?


Mathematician's algorithm:

generate all possible palindromes. Put them in an HashSet.

Look up the candidate. If it in is the set it is a Palindrome.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lord Zoltar
Guest
Posts: n/a
 
      04-02-2008
On Apr 1, 7:16*pm, "Benjamin Rushing" <(E-Mail Removed)>
wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.
> I can't use an array or use the 'reverse' function of code. *Anybody willing
> to give me a hint on how to procede?


First, calculate the probability that any given 5 digit number COULD
be a palindrome.
Then ignore the input and output yes or no with the same probability
that the input MIGHT be a palindrom.
For very large datasets, this might be sufficient!


seriously... if it's ALWAYS going to be 5 characters then it's very
easy: compare 1st and last character, 2nd and 3rd character. you could
say:
(input[0]==input[4] && input[1]==input[3]) => palindrome
you will have to find some way to split up your input string... If
you're not sure how then check through Java's string library. There's
several ways to do this.
 
Reply With Quote
 
Lord Zoltar
Guest
Posts: n/a
 
      04-02-2008
On Apr 1, 9:12*pm, Eric Sosman <(E-Mail Removed)> wrote:
> Benjamin Rushing wrote:
> > I've been tasked with writing a simple java program where the user inputs a
> > 5 digit number and the program states whether it's a palindrome or not.
> > I can't use an array or use the 'reverse' function of code. *Anybody willing
> > to give me a hint on how to procede?

>
> * * * * class DetectPalindrome {
> * * * * * * public static void main(String[] args) {
> * * * * * * * * int number = Integer.parseInt(args[0]);
> * * * * * * * * System.out.print(number + " is ");
> * * * * * * * * if (number < 0)
> * * * * * * * * * * System.out.println("not a palindrome.");
> * * * * * * * * else
> * * * * * * * * * * System.out.println("a palindrome "
> * * * * * * * * * * * * + " when written in base "
> * * * * * * * * * * * * + Math.max(number + 1L, 2));
> * * * * * * }
> * * * * }
>
> * * *There are, I suppose, less elegant approaches involving
> the % operator, or conversion to String form and comparing
> the results of carefully-chosen charAt() methods. *Ho-hum.
>
> --
> Eric Sosman
> (E-Mail Removed)


Actually, I think I like this solution the best!

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-02-2008
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>I've been tasked with writing a simple java program where the user inputs a
>5 digit number and the program states whether it's a palindrome or not.
>I can't use an array or use the 'reverse' function of code. Anybody willing
>to give me a hint on how to procede?


Slacker's solution:

Reading the number as a string is no problem. It is extra work to read
it as an int.

See http://mindprod.com/applet/fileio.html

reverse the string. Compare.

To reverse the string without using reverse, cheat, and see how
reverse does it by looking at the code in source.zip

But the point is to LEARN java, not create a palindrome detector, so
this is a stupid solution.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Palindrome using StringBuffer cat_dog_ass Java 4 01-05-2013 08:37 AM
Re: Re: Palindrome Tim Churches Python 2 06-05-2011 03:24 AM
Palindrome Runic911 Python 24 11-15-2003 12:08 AM
Re: Palindrome Pierre Quentel Python 2 11-13-2003 06:11 PM
Palindrome (HELP) Lorin Leone C++ 4 11-13-2003 08:11 AM



Advertisments