1

I have the following code that reverses an integer input. However, if I enter input with zeroes at the beginning e.g. 01234, it will return me the integer 4321 instead of 43210. I've looked at other solutions but they all solve the problem by getting the input as a String and then reversing the string. However, I need my input to be of type int.

Does anyone have any ideas how I can solve this?

public static int reverseInputNumber(int num){
    int reverse = 0;
    while(num!=0) {
        reverse = (reverse*10)+num%10;
        num /= 10;
    }
    return reverse;
}
Maroun
  • 94,125
  • 30
  • 188
  • 241

3 Answers3

2

Integers store an integer value, not an encoding of a particular representation of an integral value. So in order to really get what you want you're talking about reversing the representation of an integer.

EDIT: I thought I'd add some code to solve the solution using Strings.

public static int reverseInputNumber(int num)
{
    String reverse = StringBuilder(num + "").reverse().toString();
    return reverse;
}

Second Edit: Actually even this code won't do what you want it to do. By taking an int as a parameter you're already losing the original representation of the integer. It's accurate to say that there is no function that can take just an integer parameter and return something with the reverse of it's original representation. You would need to deal solely in strings or some other more appropriate data structure.

Varun Madiath
  • 3,152
  • 4
  • 30
  • 46
1

In java, using a zero as the most significant digit, it interpreters the number as an octal number. So 01234 gets converted to:

4 + 3 x 8 + 2 x 8^(2) + 1 x 8^(3) = 668

So you are better off with using one of the convert-to-string methods as in the other answers.

Edit: Here is a version without StringBuffer etc.:

public String getReversed(int number) {
    char[] digitArray = String.valueOf(number).toCharArray();
    String strNum = "";

    for (int i = digitArray.length - 1; i >= 0; i--) {
        strNum += digitArray[i];
    }
    return strNum;
}

2nd edit: Reversing a char array and then create the String after finished reversing, because of a possible creation of number of O(n^2) operations that could be a problem for the garbage collector.

public String getReversed(int number) {
    char[] digitArray = String.valueOf(number).toCharArray();
    char[] reversedDigitArray = new char[digitArray.length];

    for (int i = 0; i < digitArray.length; i++) {
        reversedDigitArray[i] = digitArray[digitArray.length - i - 1];
    }
    return new String(reversedDigitArray);
}

3rd Edit: Swaps chars with in-place algorithm.

public static String getReversed(int number) {
    char[] digitArray = String.valueOf(number).toCharArray();
    int count = digitArray.length - 1;
    char tmp;

    for (int i = (count-1) >> 1; i >= 0; --i) {
        tmp = digitArray[i];
        digitArray[i] = digitArray[count - i];
        digitArray[count - i] = tmp;
    }
    return new String(digitArray);
}
Bjørn Bråthen
  • 410
  • 8
  • 20
  • 1
    It might be better to simply reverse the char[] in place and then use the String(Char[]) constructor, since as it stands there is the possibility of creating a number of O(N^2) operation with a lot of garbage collection. – Varun Madiath May 07 '13 at 16:46
  • @VarunMadiath Is this better? – Bjørn Bråthen May 07 '13 at 17:20
  • 1
    I'd say it is, further improvement would be to sort it in place, but the number of digits in maximum value of int makes the penalty here too small to worry about. – Varun Madiath May 07 '13 at 22:43
  • 1
    Not a problem, I just updated my answer to describe why neither of our approaches would work. – Varun Madiath May 08 '13 at 01:14
  • I was looking through your 3rd edit, and I was wondering why you use the right shift? I can't seem to see a reason, care to explain? – Varun Madiath May 08 '13 at 01:19
  • I'm basically doing it instead of using `count / 2`, by shifting the bit i get the same effect. But I think it's faster? I have seen it before somewhere, so it's a habit I have absorbed when I iterate over half of an array. – Bjørn Bråthen May 08 '13 at 01:30
  • But maybe the readability of the code should be prioritized in this particular use case? – Bjørn Bråthen May 08 '13 at 01:37
0

If your input is required to be an int then convert it to String and reverse that.

String reverseInt(int in){
    String a = Integer.toString(in);
    return new StringBuilder(a).reverse().toString();
}

If your output is required to be an int there is no way to have a leading 0.

DeltaLima
  • 5,864
  • 1
  • 16
  • 32