C语言基本常用函数,strcmp,strcpy等实现

时间:2022-09-05 23:05:01
实现strlen
#############################################
int my_strlen(const char* str)
{
    int count = 0;
    while(*str)
    {
        count++;
        str++;
    }
    return count;
} 
***********************************************
int my_strlen(const char* str)
{
    if(*str)
    {
        return 0;
    }
    else
        return 1+my_strlen(str+1);
}
************************************************
int my_strlen(const char* str)
{
    const char *p = s;
    while(*p != '\0')
        p++;
    return p-s;
}


size_t __cdecl strlen (
        const char * str
        )
{
        const char *eos = str;

        while( *eos++ ) ;

        return( eos - str - 1 );
}
####################################################
size_t __cdecl strnlen(const char *str, size_t maxsize)
{
    size_t n;

    /* Note that we do not check if s == NULL, because we do not * return errno_t... */

    for (n = 0; n < maxsize && *str; n++, str++)
        ;

    return n;
}

#########################################################
实现strcpy

char* my_strcpy(char* dst,const char* src)
{
    char* ret = dst;
    assert(dst != NULL);
    assert(src != NULL);

    while((*dst++ = *src++))
        ;
    return ret;
}
********************************************************
char * __cdecl strncpy (char * dest,const char * source,size_t count)
{
        char *start = dest;

        while (count && (*dest++ = *source++))    /* copy string */
                count--;

        if (count)                              /* pad out with zeroes */
                while (--count)
                        *dest++ = '\0';

        return(start);
}

#########################################################
实现strcat

char* my_strcat(char* dst,const char* src)
{
    char* ret = dst;
    assert(dst!= NULL);
    assert(src != NULL);
    while(*dst )
    {
        dest++;
    }
    while(*dst++ = *src++)
        ;
    return ret;
}

*******************************************************
把src所指字符串的前n个字符添加到dest所指字符串的结尾处,并覆盖dest所指字符串结尾的'\0',从而实现字符串的连接。
src和dest所指内存区域不可以重叠,并且dest必须有足够的空间来容纳src的字符串。
char* strncat (char * front,const char * back,size_t count)
{
        char *start = front;

        while (*front++)
                ;
        front--;

        while (count--)
                if (!(*front++ = *back++))
                        return(start);

        *front = '\0';
        return(start);
}
#########################################################
实现strstr

char *strstr (const char * str1,const char * str2)
{
        char *cp = (char *) str1;
        char *s1, *s2;

        if ( !*str2 )
            return((char *)str1);

        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;

                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;

                if (!*s2)
                        return(cp);

                cp++;
        }

        return(NULL);

}
#########################################################
实现strcmp

int my_strcmp(const char* src,const char* dst)
{
    int ret = 0;
    while( !(ret = *(unsigned char *)src) - *(unsigned char
*)dst && *dst)
        ++src,++dst;
    if(ret < 0)
        ret = -1;
    else if(ret > 0)
        ret = 1;

    return ret;
}
***********************************************************
int my_strncmp(const char* first,const char* last,size_t n)
{
    int result = 0;

    if(first == NULL || last == NULL || n < 0)
        return fasle;
    if(n == 0)
        return 0;
    while(n-- &&!(result = *(unsigned char*)first - *(unsigned char*)last) && *last)
        {
        last++;
        first++;
        }
    return ret;
}


##########################################################
void* memcpy(void *dst,const void *src,size_t count)
{
    void *ret = dst;
    /* *copy from lower address to higher address */
    while(count--)
    {
        *(char*)dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }

    return ret;
}

##########################################################
void * memmove(void* dst,const void* src,size_t count)
{
    void * ret = dst;
    if(dst <= src || (char*)dst >= ((char*)src + count))
    {
    /* *non-overlapping buffers *copy from lower addresses to higher addresses */
    while(count--)
    {
        *(char*) dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }
        }
        else{
    /* *overlapping buffer *copy from higher addresses to lower addresses */
    dst = (char*)dst + count -1;
    src = (char*)src + count -1;

    while(count--)
    {
        *(char*)dst = *(char*)src;
        dst = (char*)dst - 1;
        src = (char*)src - 1;
    }
}
    return ret;
}