palindrome

  • Thread starter Benjamin Rushing
  • Start date
B

Benjamin Rushing

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?
 
P

Peter Duniho

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
 
R

Roedy Green

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.
 
E

Eric Sosman

Benjamin 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?

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.
 
L

Logan Shaw

Benjamin 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.
^^^^^^^^^^^^^^

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

- Logan
 
R

Roedy Green

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.
 
L

Lord Zoltar

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. ;)
 
L

Lord Zoltar

Benjamin 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?

        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.

Actually, I think I like this solution the best!
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top