C++中string.h自编的几个函数

今天刘老师讲了const,类型转换,函数指针的用法,以前没能认识到函数指针的强大,刘老师写排序时用到了函数指针,暧昧感觉到了函数指针的强大;

最后他留了个作业,就是写string.h里的几个函数,我写了下,感觉写的很操蛋,效率不高,明天看看老师怎么写吧!

char* strcpy(char s1[],const char s2[])
{
    char *head=s1;
    while(*s1++=*s2++);
    return head;
}

char* strncpy(char s1[],const char s2[],size_t len)
{
    char *head=s1;
    while((*s1++=*s2++)&&--len);
    *s1='\0';
    return head;
}

char* strcat(char s1[],const char s2[])
{
    char *head=s1;
    while(*s1++);
    while(*s1++=*s2++);
    return head;
}

int strcmp(const char s1[],const char s2[])
{
    while(*s1&&*s2&&*s1==*s2)*s1++,*s2++;
    return *s1-*s2;
}

size_t strlen(const char s[])
{
    const char* head=s;
    while(*s)s++;
    return s-head;
}

void *memset(void *cell,int value,size_t size)
{
    void*head=cell;
    char*p=(char*)cell;
    while(size--)*p++=value;
    return head;
}

void* memcpy(void* dest,const void* src,size_t size)
{
    void* head=dest;
    char*p=(char*)dest;
    char*q=(char*)src;
    while(size--)*p++=*q++;
    return head;
}




   

你可能感兴趣的:(C++,作业)