#include<stdio.h>
int Myatoi(char* str)
{
if (str == NULL)
{
printf("invalid input");
return -1;
}
while (*str == ' '|| *str ==' ')
{
str++;
}
int nSign =(*str == '-')?-1:1;
if (*str == '+' || *str == '-')
{
*str++;
}
int nResult = 0;
while (*str >='0' && *str <= '9')
{
nResult = nResult * 10 + (*str - '0');
*str++;
}
return nResult * nSign;
}
int main()
{
printf("%d\n", Myatoi(" -12345"));
return 0;
}
本文介绍了一个用C语言实现的atoi函数,该函数能够将字符串转换为整数,并正确处理正负号及前导空白字符。通过具体示例展示了如何使用这个函数。
912

被折叠的 条评论
为什么被折叠?



