第四阶段—数组—字符数组和字符串—字符数组:把字符串“123456”转换成数值 123456
1 #include <stdlib.h> 2 #include <stdio.h> 3 #define N 32 4 int my_atoi(char *s); 5 int main() 6 { 7 char a[N]; 8 scanf("%s", a); 9 int num = 0; 10 num = my_atoi(a); 11 printf("%d\n", num); 12 return 0; 13 } 14 int my_atoi(char *s) 15 { 16 char *p = s; 17 char c; 18 int i = 0; 19 while(c=*p++) 20 { 21 if(c>='0' && c<='9') 22 { 23 //i = i*10 + (c-'0'); 24 i = c-'0'; 25 } 26 else 27 return -1; 28 } 29 return i; 30 }
浙公网安备 33010602011771号