C语言str类型处理函数复写(笔试)

#include
#include
size_t strlen(const char *s){//字符串长度
        assert(s != NULL);
        unsigned int cnt = 0;
        while(*s++){cnt++;}
        return cnt;

}
char * strcpy(char *dest,const char *src){//字符串复制
        assert(dest != NULL && src != NULL);
        char *p = dest;
        while(*dest++ = *src++){}//先赋值后判断是否为0后++
        return p;
}
char *strncpy(char *dest,const char *src,size_t n){//字符串复制n位
        assert(dest != NULL && src != NULL);
        char *p = dest;
        while(n--&& (*dest++ = *src++)){}
        return p;
}
char *strcat(char *dest,const char *src){//字符串追加
        assert(dest != NULL && src != NULL);
        char *p = dest;
        while(*dest++){};
        --dest;
        while((*dest++ = *src++)){};
        return p;

}
char *strncat(char *dest,const char * src,size_t n){//字符串追加n位
        assert(dest !=NULL && src != NULL);
        char *p = dest;
        while(*dest++){};
        --dest;
        while((*dest++ = *src++) && n--){};
        *dest  = '\0';
        return p;
}
int strcmp(const char *s1,const char *s2){//字符串比较
        assert(s1 != NULL && s2 != NULL);
        while(*s1 != '\0' && *s2 != '\0' && *s1++==*s2++ );
        return *s1 - *s2;
}
int strncmp(const char *s1,const char *s2,size_t n){//字符串比较n位
        assert(s1 != NULL && s2 != NULL);
        while(n-->1&&*s1 != '\0' && *s2 != '\0' && *s1++ == *s2++);
        return *s1 - *s2;
}
void *memcpy(void *dest,const void *src,size_t n){//内存复制
        assert(dest != NULL && src != NULL);
        char *pdest = dest;//void数据类型不可取*操作,可转化为char,char为1位
        const char *psrc = src;
        while(n>0){
                *pdest++ = *psrc++;//赋值后++
                --n;
        }
        return dest;
}
int main(){
        double arr[3] = {1.2,1.3,1.345};
        double arr1[3] = {};
        int i;
        memcpy(arr1,arr,sizeof(arr));
        for(i = 0;i<3;i++){
                printf("%g\n",arr[i]);
        }


}

你可能感兴趣的:(C语言,字符串,指针)