模拟实现atoi,仅考虑了部分转换规则
int my_atoi(const char*p)
{
int flag = 1;
long long n = 0;
//空指针
if (p == NULL)
return 0x000000;
//空字符
if (*p == '\0')
return 0x000000;
//跳过字串前空字符
while (!(*p=='+'||*p=='-'||(*p>='0'&&*p<='9')))
{
p++;
}
//正负
if (*p++ == '-')
flag = -1;
//开始转换
while (('\0' !=*p)&& (*p >= '0' && *p <= '9'))
{
n = 10 * n + flag*(*p - '0');
if (n > INT_MAX)
return 0x000000;
p++;
}
return n;
}
int main()
{
const char *arr = "-122";
int ret = my_atoi(arr);
printf("%d\n", ret);
return 0;
}
标签:0x000000,arr,return,--,空字符,C语言,int,atoi
From: https://blog.51cto.com/u_16071993/6363338