九州天空城1:面试题 不用库函数实现itoa
来源:百度文库 编辑:偶看新闻 时间:2024/04/27 12:31:24
#include
void myitoa(int a,char * str)
{
//if(str == NULL)
// return;
int t;
int i = 0;
int len;
char buf[10];
t=a>0?a:-a;
while(t)
{
buf[i++]=t%10+'0';
t/=10;
}
len = a>0?i:++i;
buf[i]='\0';
printf("2------\n");
str[i]='\0';
while(1)
{
i--;
if(buf[len-i-1]=='\0')
{printf("3------\n");
break;
}
// printf("buf[%d]\n",len-i-1);
printf("buf[%d]\n",len-i);
str[i]=buf[len-i-1];
}
}
int main()
{
int a=123;
char str[30];
printf("1------\n");
myitoa(a,str);
printf(str);
printf("\n");
//printf("------\n");
}