# Reversing digits

#### Crusty

##### Lifer
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?

#### agnitrate

##### Diamond Member
As always recursion is the answer.

 I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]

-silver

#### damiano

##### Platinum Member
what do you mean reverse the digits?

like 12345 becomes 54321 ?

#### ggavinmoss

##### Diamond Member
This is pretty easy, think about it. Here's a hint, you'll use / and %.

-geoff

#### Crusty

##### Lifer
Originally posted by: damiano
what do you mean reverse the digits?

like 12345 becomes 54321 ?
Exactly

#### damiano

##### Platinum Member
Originally posted by: agnitrate
As always recursion is the answer.

-silver
nice

public class Reverse {
public static String reverse(String s, int pos){
String response="";
if (pos==s.length()/2){
if (s.length()%2==1)
response += s.charAt(pos);
}
else
response = s.charAt(s.length()-1-pos)+
reverse(s,pos+1) + s.charAt(pos);
return response;
}

#### Crusty

##### Lifer
Originally posted by: agnitrate
As always recursion is the answer.

 I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]

-silver
I am trying to stay away from strings, heh.

#### FeathersMcGraw

##### Diamond Member
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
java.lang.StringBuffer is your friend.

#### damiano

##### Platinum Member
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
java.lang.StringBuffer is your friend.
or the divide and mod method cited above would work

#### agnitrate

##### Diamond Member
Originally posted by: MCrusty
Originally posted by: agnitrate
As always recursion is the answer.

 I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]

-silver
I am trying to stay away from strings, heh.
Why? You're using Java, so it's not like memory efficiency is a big deal anyways. Unless you're specifically told NOT to use strings. I know that I had to do this last year because this was one of my lab exercises. I forget how exactly I implemented it but I know I used recursion because it was the easiest overall to implement. I found the above link by a simple google search 'string reverse recursion' so you'd probably get lots of hits by replacing string->integer. Good luck!

-silver

#### damiano

##### Platinum Member
Originally posted by: damiano
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
java.lang.StringBuffer is your friend.
or the divide and mod method cited above would work
mod by ten take the mod mutiply by ten the mod
mod by ten take the mod and add it to the mod before
etc...
loop it until you original number is null

#### FeathersMcGraw

##### Diamond Member
Originally posted by: agnitrate
As always recursion is the answer.
It's just as easily solvable with an iterative solution, particularly since you have a well-defined number of iterations.

#### Crusty

##### Lifer
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.

#### damiano

##### Platinum Member
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
look above the mod and add method in a loop will be faster

#### Crusty

##### Lifer
Originally posted by: damiano
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
look above the mod and add method in a loop will be faster
Yeah, im starting to write that one out .

#### FeathersMcGraw

##### Diamond Member
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
StringBuffer does not create new String objects during the append process if you preallocate the appropriate storage first.

#### damiano

##### Platinum Member
Originally posted by: MCrusty
Originally posted by: damiano
Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
look above the mod and add method in a loop will be faster
Yeah, im starting to write that one out .

I would write it for you
but you have to do your homework
and you are absolutly right
it will be more efficient that way than by using strings or stringbuffers

#### Crusty

##### Lifer
static long reverse(long a)
{
StringBuffer t = new StringBuffer();

while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}

#### Ameesh

##### Lifer
Originally posted by: ggavinmoss
This is pretty easy, think about it. Here's a hint, you'll use / and %.

-geoff
yeah this is a very typical problem.

modulo and division. and remeber what base you are in. (most likely 10)

#### Ameesh

##### Lifer
Originally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();

while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
you dont need to store it in a string, just keep everything as primitive types

#### AbsolutDealage

##### Platinum Member
Just make sure that if you are using the mod-divide method that there are going to be no decimal parts on the number... that would kinda screw you up.

#### Crusty

##### Lifer
Originally posted by: Ameesh
Originally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();

while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
you dont need to store it in a string, just keep everything as primitive types
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.

#### FeathersMcGraw

##### Diamond Member
Originally posted by: MCrusty

How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.
The result can be accumulated as an integer/long type, as long as maintaining leading zeroes is not significant.

#### damiano

##### Platinum Member
Originally posted by: FeathersMcGraw
Originally posted by: MCrusty

How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.
The result can be accumulated as an integer/long type, as long as maintaining leading zeroes is not significant.
absolutly

#### Crusty

##### Lifer
static long reverse(long a)
{
long t = 0;
while (a!=0)
{
t += (a%10);
a/=10;
t*=10;
}
t/=10;
return t;
}

I see, hehe. Thanks for all the help guys!