鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 1

[已解决]将整数转换成字符串

[复制链接]
发表于 2021-1-2 14:36:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
编写函数 void myitoa(int n, char *str),将整数转换成字符串
最佳答案
2021-1-2 14:41:14
  1. // 编写函数 void myitoa(int n, char *str),将整数转换成字符串
  2. #include <stdio.h>
  3. int myitoa(int n, char *str); //自定义函数声明
  4. int main()
  5. {
  6.     char str[20]; //定义一个数组
  7.     int m;
  8.     printf("Enter some numbers:");
  9.     scanf("%d", &m);     //输入一串数字
  10.     myitoa(m, str);      //调用函数
  11.     printf("%s\n", str); //输出字符串
  12.     return 0;
  13. }
  14. int myitoa(int n, char *str) //自定义函数
  15. {
  16.     int i;
  17.     if (!n)
  18.     return 0;
  19.     i = myitoa(n / 10, str);
  20.     str[i] = '0' + n % 10;
  21.     str[i + 1] = 0;
  22.     return i + 1;
  23. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-2 14:41:14 | 显示全部楼层    本楼为最佳答案   
  1. // 编写函数 void myitoa(int n, char *str),将整数转换成字符串
  2. #include <stdio.h>
  3. int myitoa(int n, char *str); //自定义函数声明
  4. int main()
  5. {
  6.     char str[20]; //定义一个数组
  7.     int m;
  8.     printf("Enter some numbers:");
  9.     scanf("%d", &m);     //输入一串数字
  10.     myitoa(m, str);      //调用函数
  11.     printf("%s\n", str); //输出字符串
  12.     return 0;
  13. }
  14. int myitoa(int n, char *str) //自定义函数
  15. {
  16.     int i;
  17.     if (!n)
  18.     return 0;
  19.     i = myitoa(n / 10, str);
  20.     str[i] = '0' + n % 10;
  21.     str[i + 1] = 0;
  22.     return i + 1;
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-3 17:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表