C:atoi 字符串转换成整数

int atoi(char *str)
{
    int sign=1;
    int result=0;
    //去前导空白
    while (isspace(*str)) {
        str++;
    }
    //判断正负
    if (*str=='-') {
        sign=-1;
    }
    if (*str=='+'||*str=='-') {
        str++;
    }
    //转换
    while (*str) {
        if (isdigit(*str)) {
            result=(result*10+*str-'0');
        }
        else{
            break;
        }
        str++;
    }
    return result*sign;
}

猜你喜欢

转载自blog.csdn.net/hurricane111/article/details/40544663