Instead of infinite while, with an if statement that breaks, just put the condition in the while loop.
#include <stdlib.h> #include <math.h> int reverse_int(int int_reverse) { int o = 0; while (int_reverse != 0) { o *= 10; o += int_reverse % 10; int_reverse /= 10; } return o; }
- #include <stdlib.h>
- #include <math.h>
- int reverse_int(int int_reverse) {
- int o = 0;
while (1) {o += ((int)int_reverse % 10);if (int_reverse /= 10)o *= 10;else break;- while (int_reverse != 0) {
- o *= 10;
- o += int_reverse % 10;
- int_reverse /= 10;
- }
- return o;
- }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. #include <criterion/criterion.h> // replace with the actual method being tested int reverse_int(int); Test(the_multiply_function, should_pass_all_the_tests_provided) { cr_assert_eq(reverse_int(12345), 54321); cr_assert_eq(reverse_int(-123), -321); cr_assert_eq(reverse_int(-2345), -5432); cr_assert_eq(reverse_int(0), 0); }
- // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
- #include <criterion/criterion.h>
- // replace with the actual method being tested
- int reverse_int(int);
- Test(the_multiply_function, should_pass_all_the_tests_provided) {
- cr_assert_eq(reverse_int(12345), 54321);
- cr_assert_eq(reverse_int(-123), -321);
- cr_assert_eq(reverse_int(-2345), -5432);
- cr_assert_eq(reverse_int(0), 0);
- }