• C语言中strcpy,strcmp,strlen,strcat函数原型


    #include <stdio.h>
    #include <assert.h>
    
    
    char *strcpy(char *strDest, const char *strSrc) {
        char *address=strDest;
        assert((strDest!=NULL) &&  (strSrc!=NULL));
        while(*strSrc!='\0') {
            *strDest++=*strSrc++;
        }
        *strDest='\0';
        return address;
    }
    
    char *strcat(char *strDest, const char *strSrc) {
        char *address=strDest;
        assert((strDest!=NULL) &&  (strSrc!=NULL));
        while(*strDest!='\0') {
            strDest++;
        }
        while(*strSrc!='\0') {
            *strDest++ =* strSrc++;
        }
        *strDest='\0';
        return address;
    }
    
    nt strcmp(const char *str1,const char *str2) {
        assert((*str1!='\0' && *str2!='\0'));
        while (*str1 && *str2 && *str1==*str2) {
            str1++;
            str2++;
        }
        return *str1-*str2;
    }
    
    int new_strlen(const char *str) {
        assert(str != NULL);
        int len = 0;
        while (*str++ != '\0') {
            ++len;
        }
        return len;
    }
    
    void *memcpy(void *dst, void *src, size_t n) {
        char *dp = (char*)dst;
        char *sp = (char*)src;
        assert((src!=0) && (dst!=0) && n>0);
        while (n--) {
            *(dp++) = *(sp++);
        }
        *dp='\0';
        return dst;
    }
    
    void *new_memmove(void *dst, const void *src, int n) {
        char *dp = (char *)dst;
        char *sp = (char *)src;
        assert((src != 0)&&(dst != 0) && (n > 0));
        if (sp > dp || (sp+n) < dp) {
            while(n--)
                *(dp++)=*(sp++);
            *dp='\0';
        }else if (sp < dp){
            sp += n;
            dp += n;
            *dp = '\0';
            while(n--)
                *(--dp) = *(--sp);
        }
        return dst;
    }
    
    int main() {
    
        char dest[10];
        char* src = "test!";
        char* ret = strcpy(dest, src);
        printf("Test strcpy ret is :%s\n", ret);
    
        char cat1str[20] = "cat1";
        char* cat2str = "cat2";
        printf("Test strcat ret is :%s\n", strcat(cat1str, cat2str));
    
        char *len = "len";
        printf("Test strlen ret is :%d\n", new_strlen(len));
    
        char mem_dest[20];
        char* mem_src = "the src test memcpy";
        printf("Test memcpy ret is :%s\n", memcpy(mem_dest, mem_src, 10));
    
        char mv_dest[20];
        char* mv_src = "the src test memmove";
        printf("Test memmove ret is :%s\n", new_memmove(mv_dest, mv_src, 10));
    }
    

      

  • 相关阅读:
    【leetcode】图像渲染
    【leetcode】不邻接植花
    052-75
    052-74
    052-73
    052-71
    052-70
    052-69
    052-67
    052-66
  • 原文地址:https://www.cnblogs.com/biyeymyhjob/p/2588710.html
Copyright © 2020-2023  润新知