2

It's my first question here and I'm a beginner, the code is written in C (ANSI C).

The code should return the digits as (n+1) for each digit in a recursive function.(123 -> 234; 801->912; 239->340)

The problem is when the digit 9 appears and the code adds it 1 the result is 10 and it's required to become 0. Is there a way to handle it without checking specially for the digit 9?

Thanks!

int swap(int num)
{
    int a,b;
    if (num/10==0)
        return num+1;
    else
    {
        a = num % 10;
        a++;
        b = swap(num / 10);
        return (b * 10) + a;
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
AG19
  • 27
  • 4

4 Answers4

2

In order to get the next digit without checking you just have to wrap around using MOD operator %. So a = (num % 10 + 1) % 10 or even simpler a = (num + 1) % 10 as pointed out by Michael

The Javatar
  • 695
  • 5
  • 15
  • Couldn't it just be written as `a = (num + 1) % 10;` ? – Michael Dec 14 '19 at 08:09
  • 2
    @Michael Corner: `(num + 1) % 10` can overflow. `(num % 10 + 1) % 10` never overflows. – chux - Reinstate Monica Dec 14 '19 at 08:18
  • If you want '0000' as a result, wouldn't it be better to use the user input directly (string)? Otherwise the `swap()` caller needs to use some `printf %N0d` ... – Déjà vu Dec 14 '19 at 08:21
  • Thanks for the quick response! I've deleted my previous comment, sorry for the mess. let's assume the user has entered 9999, the required result would be 0 and not 10000. – AG19 Dec 14 '19 at 08:25
2

It seems you mean the following

#include <stdio.h>

unsigned int increase_digits( unsigned int n )
{
    const unsigned int Base = 10;

    return ( n + 1 ) % Base + ( n / Base == 0  ? 0 : Base * increase_digits( n / Base ) ); 
}

int main(void) 
{
    unsigned int n = 0;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 9;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 13;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 801;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 239;

    printf( "%u: %u\n", n, increase_digits( n ) );

    return 0;
}

The program output is

0: 1
9: 0
13: 24
801: 912
239: 340
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • How about OP's [9999](https://stackoverflow.com/questions/59333372/c-turn-9-to-0#comment104865315_59333396)? – chux - Reinstate Monica Dec 14 '19 at 08:20
  • @chux-ReinstateMonica It will be 0. – Vlad from Moscow Dec 14 '19 at 08:21
  • 1
    While `Base` provides the appearance genericity, it seems out of place as a constant defined in the function body. I might be tempted to reverse the sense of the test, so as to remove the `== 0` part, and change the test to be `n /= Base` so that the expression is not repeated in the recursive call (of course, I would have to check if I need to add a sequence point for the initial `(n + 1) % Base`). Upvote nonetheless. – jxh Dec 14 '19 at 08:59
2

Recursively, the simplest approach would be:

unsigned swap (unsigned n) {
    if (n < 10) return ++n % 10;
    return 10 * swap(n/10) + swap(n%10);
}

What this function says is:

  • If n is less than 10, the result is 1 larger than its current value, modulo 10.
    Thus, 9 would become (10 mod 10), which would be 0.
  • Otherwise, recursively apply the algorithm against the last digit and the rest of the digits.
    The trick here is that the rest of the digits is obtained by dividing the original number by 10, and multiplying the received result by 10.
jxh
  • 69,070
  • 8
  • 110
  • 193
2

A version that prints 00 for an input of 99

int swap(int num) {
     return num/10 ?
        swap(num/10)*10 + ((num % 10 + 1)%10) :
        (num % 10 + 1) % 10;
}

The swap caller function determines the length of the integer parameter, then displays any necessary leading zeroes.

void caller(int num) {
     char s[20];             // Assume an int as at most 19 chars...
     sprintf (s, "%d", num); // Could also use log or a loop...

     printf("result: %0*d\n", (int)strlen(s), swap(num));    
}

Use it as

caller(mynumber);
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • Thank you! it would be very helpful for me. – AG19 Dec 14 '19 at 08:39
  • 1
    In the case of a leading 9 turning into a leading 0, I might want to represent that condition with a negative value (like `93` -> `-104`), and the caller figures out the negative value means the leading digit needs to be removed after changing signs. Upvote. – jxh Dec 14 '19 at 09:07