这道题目其实就是讲字符串转化为整型数字的一道题目,在Linux C函数库中,我们可以直接调用atoi函数进行操作,在这里我们需要有自己的atoi函数。根据题意分析,和之前所写的atoi函数有所不同,我们需要判断的是这个字符串中数字区可能是不连续的,所以我们要用while语句而不是之前的for语句。
int atoi(char str[])
{
int num = 0;
char *p = str;
if(*str == '-')
{
flag = -1;
str++;
}
while(*p != '\0')
{
if(*p >= '0' && *p <= '9')
{
num = num * 10 + *p - '0';
}
p++;
}
printf("num = %d\n",num);
}