Reversing digits

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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
Jul 2, 2001
3,761
1
0
As always recursion is the answer.

[edit] 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
May 29, 2002
2,322
1
0
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
Sep 30, 2001
12,684
2
81
Originally posted by: agnitrate
As always recursion is the answer.

[edit] 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
Oct 17, 2001
4,041
1
0
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
May 29, 2002
2,322
1
0
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
Jul 2, 2001
3,761
1
0
Originally posted by: MCrusty
Originally posted by: agnitrate
As always recursion is the answer.

[edit] 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
May 29, 2002
2,322
1
0
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
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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
May 29, 2002
2,322
1
0
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
Sep 30, 2001
12,684
2
81
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 .

:p
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
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
May 29, 2002
2,322
1
0
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 .

:p

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
Sep 30, 2001
12,684
2
81
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
Apr 3, 2001
23,686
0
0
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
Apr 3, 2001
23,686
0
0
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
Dec 20, 2002
2,675
0
0
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
Sep 30, 2001
12,684
2
81
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
Oct 17, 2001
4,041
1
0
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
May 29, 2002
2,322
1
0
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
Sep 30, 2001
12,684
2
81
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!