strcmp

char* strcpy(char* dst,const char* src)

strncpy

char* strncpy(char* dst,const char* src,int n)  
char* strncpy(char* dst,const char* src,int n)
{  
    assert(dst != NULL && src != NULL);  
    char* ret = dst;  
    while (n--) {  
        *dst++ = *src++;  
    }  
    return ret;  
} 

strcat

char* strcat(char* dst,const char* src);  
char* strcat(char* dst,const char* src) {  
    assert( dst!= NULL && src != NULL);  
    char *ret = dst;  
    while (*dst) {
        dst++;  
    }
    while ((*dst++ = *src++) != '\0');  
    return ret;  
}

strncat

char* strncat(char *dst, const char *src, int n);
char* strncat(char *dst, const char *src, int n) 
{  
    assert(dst!= NULL && src != NULL);  
    char *ret = dst;  
    while (*dst) { 
        dst++;  
    }
    while (n--) {
        *dst++ = *src;
        if (*src == '\0') { break;}
        src++;  
    }  
    return ret;  
} 

strcmp

strcmp(const char* dst,const char* src);
int strcmp(const char* dst,const char* src) 
{  
    assert(dst != NULL && src != NULL);  
    
    while (*dst != '\0' && *des != '\0') {
        if (*dst != *src) {
               return -1;
        } else {
            ++dst;
            ++src;
        }
    }
    return *dst == *src ? 0 : -1;
}

strncmp

strncmp(const char* dst,const char* src, size_t n);

strstr

char* strstr(const char *source, const char *target);
char *strstr(const char *str1, const char *str2)
{
    char *cp = (char*)str1;
    char *s1, *s2;
 
    if (*str2 == '\0') { return ((char *)str1); }
 
    while (*cp) {
        s1 = cp;
        s2 = (char *) str2;
 
        while (*s1 && *s2 && !(*s1 - *s2)) {
            s1++;
            s2++;
        }
 
        if (!*s2) { return (cp); }
        
        cp++;
    }
    return (NULL);
}

atoi

int atoi(const char* str);
int atoi(const char* str) {
    if (str == NULL || str == '\0') { // 边界
        return 0;
    }
    while (*str != '\0' && *str == ' ') {  // 多余空格
        ++str;
    }
    int res = 0;
    int flag = 1;
    if (*str == '-') { // 符号
        flag = -1;
        ++str;
    } else if (*str == '+') {
        ++str;
    } else if (*str > '9' || *str < '0') {
        return res;
    }
    while (*str != '\0') {
        if (*str >= '0' && *str <= '9') {
            res = res * 10;
            res += flag * (*str - '0');
        } else {
            break;
        }
        ++str;
    }
    return res; 
}

shiyang6017
158 声望59 粉丝

引用和评论

0 条评论