# reversing digits

• 08-07-2011, 06:28 PM
mamoonrizwan
reversing digits
how do we reverse the digits .
like if i were given 12345
and i were to convert it to 54321.
• 08-07-2011, 06:36 PM
eRaaaa
There are many ways. You have no idea? And do you have/(want the result) the digit as an integer or as a string?
• 08-07-2011, 06:51 PM
mamoonrizwan
reversong digits
integer.
actuallu i wanna make GUI reverse number program
• 08-07-2011, 06:52 PM
eRaaaa
System.out.println(new StringBuilder(String.valueOf(12345)).reverse());
or use the mathematic :)
Code:

```                                int digit = 12345;                                 while (digit != 0) {                                         int tmp = digit % 10;                                         digit /= 10;                                         System.out.print(tmp);                                 }```
or or or....no own further ideas? ;(
• 08-07-2011, 06:56 PM
Norm
The GUI to get the number and show the reverse is one thing to code.
The code to do the reversing is another thing.
They can be done separately.
In the example you posted, does it make any difference if the characters are numeric digits: 0-9 or can they be anything like letters or special characters: A, b, { etc
• 08-07-2011, 09:18 PM
JosAH
What should be the reverse of, say, 100? 001? or 1?

kind regards,

Jos
• 08-11-2011, 09:28 PM
mamoonrizwan
could you provide me with the code
• 08-11-2011, 09:35 PM
Norm
No, but we will help you get your code working.
What do you have now? What is your algorithm or design for solving this problem?
• 08-11-2011, 09:35 PM
JosAH
Quote:

Originally Posted by mamoonrizwan
could you provide me with the code

What would be the use of that? We give you the code; you turn it in as if you wrote it; you get a fine grade; you get a job somewhere and we end up having to do your work because you know zilch about programming. capiche? It would be much better if you give it a try on your own; if you get stuck come back here and we'll try to help you out.

kind regards,

Jos
• 08-12-2011, 08:02 AM
stchman
Do it via the string route.

Psuedo code.

Declare String initializing it with int // hint Integer class
Declare temp String
Loop through String length in reverse
Concatenate temp with charAt

The temp String will be the reverse of the int String.
• 08-12-2011, 09:14 AM
JosAH
Quote:

Originally Posted by stchman
Do it via the string route.

Psuedo code.

Declare String initializing it with int // hint Integer class
Declare temp String
Loop through String length in reverse
Concatenate temp with charAt

The temp String will be the reverse of the int String.

If you want to do it with Strings (reversing 100 yields 001) don't do it the clumsy way; read what the StringBuilder class can do for you.

kind regards,

Jos
• 08-12-2011, 11:50 PM
stchman
Quote:

Originally Posted by JosAH
If you want to do it with Strings (reversing 100 yields 001) don't do it the clumsy way; read what the StringBuilder class can do for you.

kind regards,

Jos

I forgot the include Integer.parseInt( temp ), that would remove the leading zeroes.
• 08-13-2011, 09:07 AM
JosAH
Quote:

Originally Posted by stchman
I forgot the include Integer.parseInt( temp ), that would remove the leading zeroes.

That's not what I mean; the StringBuilder class has a reverse() method so no 'manual' String reversal is needed. Read its API documentation.

kind regards,

Jos