您的位置:首页 > 其它

itoa函数简单实现

2015-03-02 22:29 288 查看






实现itoa函数


<span style="font-size:18px;">#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void int_str(int n, char* str)
{
int sign = n, i = 0, j = 0;   //sign用来标记,判断输入的n大于或小于0
char temp[10];
n = sign < 0 ? -n : n;
do
{
temp[i] = n % 10 + '0';
n /= 10;
i++;
} while (n>0);       //循环结束i比数组有效字符个数大一
if (sign < 0)
{
temp[i++] = '-';
}
temp[i] = '\0';
i--;
while (i >= 0)
{
str[j++] = temp[i--];
}
str[j] = '\0';
}

void main()
{
int n;
char str[10];
printf("Please intput integer:");
scanf("%d", &n);
int_str(n, str);
printf("Output:%s\n", str);
system("pause");
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: