面试之自定义字符串操作

面试之自定义字符串操作

  1. strcpy

    #include <assert.h>
    char *strcpy(char *dest, const char *src)
    {
    	assert( (dest != NULL) && (src != NULL);
       	char *address = dest;
        while((*dest++ = *src++) != '');
        return address;
    }
     
    
  2. strcmp

    #include <assert.h>
    
    
    int strcmp(const char *str1, const char *str2)
    {
        assert((str1 != NULL) && (str2 != NULL));
        for (; (*str1) && (*str2) && (*str1 == *str2);++ str1,++ str2);
        return (*str1 - *str2);
    }
    
  3. strcat

    char *m_strcat(char *dest, const char *src)
    {
        assert((dest != NULL) && (src != NULL));
        char *add = dest;
        while (*dest != '\0')
            ++dest;
        while (*dest++ = *src++);
        return add;
    }
    
  4. strlen

    unsigned int m_strlen(const char *str)
    {
        assert(str != NULL);
        int len = 0;
        while (*str != '\0')
        {
            ++str;
            ++len;
        }
        return len;
    }
    
posted @ 2020-08-21 15:01  Chilk  阅读(113)  评论(0编辑  收藏  举报