实现一:
#include <assert.h>
int my_atoi(const char * str)
{
assert(str);
int ret = 0;
int flag = 1;
if(*str == '-'){
flag = -1;
str ++;
}else if(*str == '+'){
str++;
}
while(*str){
unsigned int ch = str[len] - '0';
assert(ch<=9 && ch >=0);
ret = ret * 10 + ch;
}
return ret * flag;
}
int my_atoi(const char * str)
{
assert(str);
int ret = 0;
int flag = 1;
if(*str == '-'){
flag = -1;
str ++;
}else if(*str == '+'){
str++;
}
while(*str){
unsigned int ch = str[len] - '0';
assert(ch<=9 && ch >=0);
ret = ret * 10 + ch;
}
return ret * flag;
}
实现二:
int myatoi(const char* str)
{
int i=0,j=0,k=1;
if(str[j] == '-') {
k = -1;
j++;
}
if(str[j] == '+') {
j++;
}
while(!isalpha(str[j]) && j<strlen(str))
{
i = i*10+ (str[j] - '0');
j++;
isalpha()函数说明:
原型:extern int isalpha(int c);
用法:#include <ctype.h>
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。