烟火的你 2021-05-08 00:05 采纳率: 100%
浏览 20
已采纳

老哥懂行吗?运行出问题,得不到想要的结果。

老哥懂行吗?运行出问题,得不到想要的结果。
  • 写回答

4条回答 默认 最新

  • benbenli 2021-05-08 02:04
    关注

    哦,题目要求些一个函数。下面代码写了一个函数,而且不限于4位数。

    #include <stdio.h>
    
    char* digits(int number, char* buffer, int size)
    {
        char* p = buffer + (size - 1);
        *p = 0; // end of string
        
        if (number == 0)
        {
            *(--p) = '0';
        }
        else
        {
            while (number > 0)
            {
                int d = number % 10;
                number /= 10;
                if (*p != 0)
                    *(--p) = ' ';
                *(--p) = '0' + d;
            }        
        }
    
        return p;
    }
    
    int main()
    {
        int n;
        char s[100];
        
        printf("Please enter a 4-digit number:");
        scanf("%d", &n);
        
        char* p = digits(n, s, 100);
        
        printf("The digits of the number are: %s\n", p);
    }
    
    // Output
    Please enter a 4-digit number:1990                                                                                                                                                 
    The digits of the number are: 1 9 9 0
    
    Please enter a 4-digit number:31415926                                                                                                                                             
    The digits of the number are: 3 1 4 1 5 9 2 6    

    附注:求赞助积分和C币。加入CSDN将近20年了。最近几年忙小孩没登录。刚才搜索到一本电子书想下载,需要20积分/C币。已经收到8元了,还查12元。赞助多少都可以。多谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?