C语言中常用到的字符串函数

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

字节拷贝函数:
void * memcpy1(void *dest, const void *src, size_t n)
{
        assert(dest!=NULL&&src!=NULL);    //有效性检测
        if(dest == src)
                return dest;
        else if(dest < src)
        {
                const char *c_src = static_cast<const char *>(src);
                char *c_dest = static_cast<char *>(dest);
                while (n--) *c_dest++ = *c_src++;
        }
        else
        {
                const char *c_src = static_cast<const char *>(src) + n - 1;
                char *c_dest = static_cast<char *>(dest) + n - 1;
                while (n--) *c_dest-- = *c_src--;
        }
        return dest;
}


 
字符串比较函数:
char *strcpy(char *des, const char *src)  
{  
    assert(des != NULL && src != NULL);  
    char *pStr = des;  
    while( (*des++ = *src++) != '\0')  
        NULL;  
    return pStr;  
}  


字符串比较函数:
int  strcmp(const char *des,  const char *src)  
{  
    assert(des != NULL && src != NULL);  
    for (;*des && *src && *des == *src; des++, src++)  
        NULL;  
    return *des - *src;  
}  


字符串长度函数:
int strlen(const char *pStr)  
{  
    assert(pStr != NULL);  
    int len = 0;  
    while (*pStr++ != '\0')  
        len++;  
  
    return len;  
}  


字符串连接函数:
char *strcat(char *des, const char *src)  
{  
    assert(*des != NULL && *src != NULL);  
    char *pStr = des;  
    while (*des != '\0')  
        des++;  
  
    while ((*des++ = *src++) != '\0')  
        NULL;  
    return pStr;  
}

 
字符串查找函数:
char *myStrstr(const char *des, const char *src)  
{  
    assert(des != NULL && src != NULL);  
  
    char *d = des;  
    char *s = src;  
  
    for (; d != '\0'; des++)  
    {  
        for (d = des, s = src; (s != '\0') && (*d == *s); d++, s++)  
            NULL;  
      
    if (*s == '\0')  
        return des;  
    }  
      
    return NULL;  
  
}