Chinaunix首页 | 论坛 | 博客
  • 博客访问: 844494
  • 博文数量: 156
  • 博客积分: 6553
  • 博客等级: 准将
  • 技术积分: 3965
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 18:36
文章存档

2012年(3)

2011年(43)

2010年(110)

分类: C/C++

2010-10-26 17:15:10

二         itoa       把一整数转换为字符串

例程序:

#include

#include

void        itoa (int n,char s[]);

//atoi 函数:将s转换为整形数

int main(void )

{   

int n;

char s[100];

printf("Input n:\n");

scanf("%d",&n);

          printf("the string : \n");

          itoa (n,s);

return 0;

}

void itoa (int n,char s[])

{

int i,j,sign;

if((sign=n)<0)//记录符号

        n=-n;//使n成为正数

          i=0;

do{

        s[i++]=n%10+'0';//取下一个数字

}while ((n/=10)>0);//删除该数字

if(sign<0)

        s[i++]='-';

s[i]='\0';

for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出

        printf("%c",s[j]);

}

阅读(5503) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-10-27 17:34:33

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com