将一个整数转换为字符串
【摘要】 #include <stdio.h>#include <string.h>#include <malloc.h> void reverse(char *str){ char temp; char *last_char; for (last_char = str; *last_char != '\0'; last_char++) ; l...
-
#include <stdio.h>
-
#include <string.h>
-
#include <malloc.h>
-
-
-
void reverse(char *str)
-
{
-
char temp;
-
char *last_char;
-
-
for (last_char = str; *last_char != '\0'; last_char++)
-
;
-
last_char--;
-
-
while (str < last_char)
-
{
-
temp = *str;
-
*str++ = *last_char;
-
*last_char-- = temp;
-
}
-
}
-
-
char* itooa(int num)
-
{
-
int len = 0;
-
char *s = NULL;
-
int i=0,j;
-
int sum = num;
-
while (sum != 0)
-
{
-
sum /= 10;
-
len++;
-
}
-
s = (char *)malloc(len+1);
-
while(num != 0)
-
{
-
j = num % 10;
-
num /= 10;
-
s[i] = j + '0';
-
i++;
-
}
-
s[i] = '\0';
-
reverse(s);
-
return s;
-
}
-
-
-
int main()
-
{
-
int i;
-
char *str;
-
printf("请输入一个整数:");
-
scanf("%d",&i);
-
str = itooa(i);
-
-
printf("您输入的整数数转换为字符串是:%s\n",str);
-
reverse(str);
-
printf("将该字符串反转后的字符串是:%s\n",str);
-
return 0;
-
-
}
-
-
-
-
-
-
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/11760683
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)