c函数重写

strlen()函数

unsigned int mstrlen(const char *s){
    const char *ss = s; 
    while (*ss)
        ss++;
    return ss - s;
}

strncmp()函数

int mstrncmp(const char *s1, const char *s2, int n){  
    const unsigned char *c1 = (const unsigned char *)s1;
    const unsigned char *c2 = (const unsigned char *)s2;
    unsigned char ch;
    int d = 0;
    while (n--) {
        d = (int)(ch = *c1++) - (int)*c2++;
        if (d || !ch)
            break;
    }
    return d;
}

strcpy()函数

char* mstrcpy(char* str,const char* ptr)
{
assert(str!=NULL&&ptr!=NULL);
char *p=str;
while((*str++=*ptr++)!='\0');
return p;
}

strcat()函数

char* mstrcat(char* str,const char*ptr)
{
assert(str!=NULL&&ptr!=NULL);
char* p=str;
while(*str!='\0')str++;
while((*str++=*ptr++)!='\0');
return p;
}

上一篇:分布式全局ID生成器-雪花算法


下一篇:[luogu]P6225 [eJOI2019]异或橙子 树状数组