【C语言】数目字的字符串转化为 数字

【C语言】数字的字符串转化为 数字
#include <stdio.h>


/*
这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
如果字符串参数包含了任何非数字字符,函数就返回零。

*/

int ascii_to_integer(char *str)
{
int i=0;
while(*str!='\0')
{
if(*str<'0'|| *str >'9')
return 0;
i*=10;              //进位
i+=*str-'0';
str++;
}
return i;
}

int main()
{
char s[100]={0};
scanf("%s",s);
printf("%d\n",ascii_to_integer(s));


return 0;
}