I'm writing this code for more than 3 hours already..
I gave up about the overflow thing and tried to google and look it up on stackoverflow.
I did not find any solution besides the one that I wrote in my code as you can see in lines 27-28 (where it returns 0).
But this condition also does not work.
#include <stdio.h>
int reverse(int x) {
int pos = 0;
int reversed = 0;
int numOfDigits = 0;
int tenPower = 1;
if (x < 0) {
pos = -x;
} else
pos = x;
while (pos > 0) {
pos = (pos - (pos % 10)) / 10;
numOfDigits++;
}
while (numOfDigits > 0) {
for (int i = numOfDigits - 1; i > 0; i--) {
if (numOfDigits == 1)
tenPower = 1;
else
tenPower *= 10;
}
//overflow check - does not work
if (x % 10 != 0 && ((x % 10) * tenPower) / (x % 10) != tenPower)
return 0;
reversed += (x % 10) * tenPower;
numOfDigits--;
x = (x - (x % 10)) / 10;
tenPower = 1;
}
if (x < 0)
return -reversed;
else
return reversed;
}
int main() {
int arr[5] = {-30, 120, 1501, 321, 0};
for (int i = 0; i < 5; i++) {
printf("Original number is: %d \n", arr[i]);
printf("Reversed number is: %d \n", reverse(arr[i]));
}
}
The input that is not working due to overflow is:
1534236469
The error code on leetcode is
Line 25: Char 28: runtime error: signed integer overflow:
1000000000 * 9 cannot be represented in type 'int' [solution.c]Line:
if (x%10 != 0 &&((x%10)*tenPower) / (x%10) != tenPower)
Other than that, the code is working and every number (positive & negative numbers) is being successfully reversed.
I'll be glad to hear you out about a possible solution and also let me know what do you think about my code and the way I decided to complete this task, I know that's the most basic and naïve way to do it, but I'll be glad to know how could I improve it.
The task is:
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Examples:
Input: x = 123 Output: 321, Input: x=-120 Output = -21
long intor as the answer from @rickimaru shows you can cast the integer value to a long integer and check to see if the result can still fit back into an integer value. - pseudovella