str系列函数的实现

来源:互联网 发布:mac app cleaner 编辑:程序博客网 时间:2024/05/16 08:22
//atoi#include <stdio.h>#include <assert.h>#include <ctype.h>int my_atoi(char *pstr){    assert(pstr != NULL);    int ret_int = 0;    int sign = 1;    while(isspace(*pstr)){        pstr++;    }    if(*pstr == '-' || *pstr == '+' || (*pstr > '0' && *pstr < '9')){        if(*pstr == '-' || *pstr == '+'){            if(*pstr == '-'){                sign = -1;            }            pstr++;        }        if(!(*pstr >= '0' && *pstr <= '9')){            printf("the str is not ok\n");            return -1;        }            while(*pstr >= '0' && *pstr <= '9'){            ret_int = ret_int * 10 + *pstr - '0';            pstr++;        }        if(*pstr != '\0'){            printf("the str is not ok\n");            return -1;        }        ret_int = sign * ret_int;    }else{        printf("the str is not ok\n");        return -1;    }    return ret_int;}int main(int argc, char **args){    char a[] = "-a100a";    char b[] = "500b";    int aa = my_atoi(a);    int bb = my_atoi(b);    printf("%d %d\n", aa, bb);    return 0;}

0 0
原创粉丝点击