常用库函数strcpy,strcmp,strcat,memset,memcpy,memcmp原型

char *strcpy(char *dest, const char *src)
{
          unsigned i;
          for (i=0; src[i] != '\0'; ++i)
            dest[i] = src[i];
        
          //Ensure trailing null byte is copied
          dest[i]= '\0';
        
          return dest;
}
        
char *strcpy(char *dest, const char *src)
{
           char *save = dest;
           while(*dest++ = *src++);
           return save;
}
    
int strcmp (const char * s1, const char * s2)
{
        for(; *s1 == *s2; ++s1, ++s2)
            if(*s1 == 0)
                return 0;
        return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}

char * strcat(char *dest, const char *src)
{
    size_t i,j;
    for (i = 0; dest[i] != '\0'; i++)
        ;
    for (j = 0; src[j] != '\0'; j++)
        dest[i+j] = src[j];
    dest[i+j] = '\0';
    return dest;
}

void *memset(void *s, int c, size_t n)
{       /* store c throughout unsigned char s[n] */
        const unsigned char uc = (unsigned char)c;
        unsigned char *su = (unsigned char *)s;

        for (; 0 < n; ++su, --n)
          *su = uc;
        return (s);
}

void *memcpy(void *s1, const void *s2, size_t n)
{       /* copy char s2[n] to s1[n] in any order */
        char *su1 = (char *)s1;
        const char *su2 = (const char *)s2;

        for (; 0 < n; ++su1, ++su2, --n)
          *su1 = *su2;
        return (s1);
}

int memcmp(const void *s1, const void *s2, size_t n)
{       /* compare unsigned char s1[n], s2[n] */
        const unsigned char *su1 = (const unsigned char *)s1;
        const unsigned char *su2 = (const unsigned char *)s2;

        for (; 0 < n; ++su1, ++su2, --n)
          if (*su1 != *su2)
            return (*su1 < *su2 ? -1 : +1);
        return (0);
}

你可能感兴趣的:(C/C++)