问题

将数字字符串转换成整数

算法分析

将数字字符串转换成整数_i++


算法步骤如下:

将数字字符串转换成整数_i++_02

算法实现

#include <stdio.h>
int atoi(char s[ ]);
main()
{
char s[20];
scanf("%s", s);
printf("%d\n", atoi(s));
}
int atoi(char s[ ])
{
int i, n, sign;
for(i=0; s[i] = = ' ' || s[i] = = '\n' || s[i] = = '\t'; i++)
; /* skip white space */
sign = 1;
if(s[i] = = '+' || s[i] = = '-')
sign = (s[i++] = = '+')?1:-1;
for(n=0; s[i] >= '0' && s[i] <= '9'; i++)
n = 10*n + s[i] '0';
return ( sign * n);
}