c program for the reverse the digits

后端 未结 8 1000
北恋
北恋 2021-01-07 11:27

I am looking for the C program for reverse the digits like below:

If i enter:

123456

Then the result would

8条回答
  •  庸人自扰
    2021-01-07 11:56

    Here is another possibility. It is non-recursive, and perhaps a little less code. There is an example in the code comments to explain the logic.

    /*
        Example: input = 12345
    
        The following table shows the value of input and x
        at the end of iteration i (not in code) of while-loop.
        ----------------------
        i   |   input  |    x
        ----------------------
        0       12345       0
        1        1234       5
        2         123      54
        3          12     543
        4           1    5432
        5           0   54321
        ----------------------
    */
    uint32_t
    reverseIntegerDigits( uint32_t input )
    {
        uint32_t x = 0;
    
        while( input )
        {
            x = 10 * x + ( input % 10 );
            input = input / 10;
        }
    
        return x;
    }
    

提交回复
热议问题