Omar Alaa
asked on
C# Reverse int in fast ways
i have been looking for a way to reverse an integer and i have found this
so i tried to code it myself and i have been to this
while(left>0)
{
r = left % 10;
rev = rev * 10 + r;
left = left / 10;
}
but the problem is this code is extremely slow. because it will only be a very small part of big operation. so i tried to code it myself and i have been to this
string[] userinput = Console.ReadLine().Split(' ');
int i = int.Parse(userinput[0]);
int j = int.Parse(userinput[1]);
int k = int.Parse(userinput[2]);
int Reverse = 0;
char[] temp;
string trans = i.ToString();
temp = trans.ToCharArray();
temp.Reverse();
trans = new string(temp);
Reverse = int.Parse(trans);
Console.WriteLine(Reverse);
but it always gives the value 0 "about the j and k forget about them its just for the rest of my code" so can anyone help me how can i make it work and if this can't be done any new ways to how to make it in quicker way than the shown above because it's really extremely slow.
Didn't get the task...
ASKER
I meant for example if the int value is 5273 i want it to be 3725
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay. Thanks to letting me know that :D
Cause "reverse an int" in our given context depends on the format (base). E.g. 0xef is also an int. What is its "reverse" value? It seems to depend on the base..
So your loop maybe already the fastest way to do it in .NET.
Caveat: an int could be lesser then 0, then your code does not work.