整数转字符串

来源:互联网 发布:sql 给字段设置默认值 编辑:程序博客网 时间:2024/05/22 13:22

将输入的整数转化为字符串。输入:整数 。输出:指向字符串的指针

函数原型:char *shuzi2zifu(int n)

 

#include<stdio.h>#include<string.h>#include<stdlib.h>char *shuzi2zifu(int n){char *p;int count=0;int i=0;int temp;int flag;if(n<0){flag=1;n=-n;}else flag=0;int m=n;while(n){n=n/10;count++;}printf("%d\n",count);p=(char *)malloc(sizeof(char)*count);n=m;while(n){p[i++]=n%10+'0';n=n/10;}if(flag==1){p[i++]='-';count++;}p[i]='\0';for(int j=0;j<count/2;j++){temp=p[j];p[j]=p[count-1-j];p[count-1-j]=temp;}return p;}void main(){char *p;int n;printf("please input the data:");scanf("%d",&n);p=shuzi2zifu(n);puts(p);}



运行结果:

please input the data:349730
6
349730

 

please input the data:-7862
4
-7862

原创粉丝点击