1. Member
Join Date
Apr 2011
Posts
19
Rep Power
0

## reversing digits

how do we reverse the digits .
like if i were given 12345
and i were to convert it to 54321.

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
There are many ways. You have no idea? And do you have/(want the result) the digit as an integer or as a string?

3. Member
Join Date
Apr 2011
Posts
19
Rep Power
0

## reversong digits

integer.
actuallu i wanna make GUI reverse number program

4. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
System.out.println(new StringBuilder(String.valueOf(12345)).reverse());
or use the mathematic :)
Java Code:
```				int digit = 12345;
while (digit != 0) {
int tmp = digit % 10;
digit /= 10;
System.out.print(tmp);
}```
or or or....no own further ideas? ;(

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

6. What should be the reverse of, say, 100? 001? or 1?

kind regards,

Jos

7. Member
Join Date
Apr 2011
Posts
19
Rep Power
0
could you provide me with the code

What do you have now? What is your algorithm or design for solving this problem?

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

10. 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.
Last edited by stchman; 08-12-2011 at 08:05 AM.

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

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

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•