----------------------------------
典型例题 30:C问题---itoa函数
----------------------------------
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void itoa( int i,char* string)
5 {
6 int power, j;
7 if(i<0)
8 {
9 i = -i;
10 *string++ = '-';
11 }
12 j=i;
13 for (power=1;j>=10;j/=10)
14 power*=10;
15 for (;power>0;power/=10)
16 {
17 *string++='0'+i/power; i%=power;
18 }
19 *string='\0';
20 }
21
22 void itostr(int value,char *str)
23 {
24 sprintf(str, "%d",value );
25 return;
26 }
27 int main()
28 {
29 int n;
30 char str[13];
31 printf("Please input the int num:\n");
32 scanf("%d",&n);
33 itostr(n,str);
34 printf("(1)int to string :%s\n",str);
35 itoa(n,str);
36 printf("(2)int to string :%s\n",str);
37 return 0;
38 }
--------------------
$ ./a.out
Please input the int num:
-1235698
(1)int to string :-1235698
(2)int to string :-1235698
--------------------
知识点:整数转换为字符串两种方法,(1)整数分拆法,(2)sprintf()法;
典型例题 30:C问题---itoa函数
----------------------------------
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void itoa( int i,char* string)
5 {
6 int power, j;
7 if(i<0)
8 {
9 i = -i;
10 *string++ = '-';
11 }
12 j=i;
13 for (power=1;j>=10;j/=10)
14 power*=10;
15 for (;power>0;power/=10)
16 {
17 *string++='0'+i/power; i%=power;
18 }
19 *string='\0';
20 }
21
22 void itostr(int value,char *str)
23 {
24 sprintf(str, "%d",value );
25 return;
26 }
27 int main()
28 {
29 int n;
30 char str[13];
31 printf("Please input the int num:\n");
32 scanf("%d",&n);
33 itostr(n,str);
34 printf("(1)int to string :%s\n",str);
35 itoa(n,str);
36 printf("(2)int to string :%s\n",str);
37 return 0;
38 }
--------------------
$ ./a.out
Please input the int num:
-1235698
(1)int to string :-1235698
(2)int to string :-1235698
--------------------
知识点:整数转换为字符串两种方法,(1)整数分拆法,(2)sprintf()法;