C语言实现strcpy strcat strstr strchr strcmp memcpy memove

char my_strcpy(char *dest, const char *src)//实现strcpy
{
    assert(dest);
    assert(src);
    while (*src)
    {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';
}

int main()
{
    char dest[20] ="1234567890";
    char src []= "abcdef";
    my_strcpy(dest, src);
    printf("%s\n", dest);
    system("pause");
    return 0;
}
#include
#include

char my_strcat(char *dest, const char *src)//实现strcat
{
    assert(dest);
    assert(src);
    while (*dest != '\0')
    {
        dest++;
    }
    while (*src != '\0')
    {
        *dest = *src;
        dest++;
        src++;
    }
}
int main()
{
    char dest[20] = "1234567890";
    char src []= "abcdef";
    my_strcat(dest, src);
    printf("%s", dest);
    system("pause");
    return 0;
}
#include
const char *Mystrchr(const char *src, char ch)//实现strchr
{
    while (*src != '\0')
    {
        if (*src == ch)
        {
            return src;
        }
        src++;
    }
    return NULL;
}
int main()
{
    char str[10] = "abcdefg";
    char a = 'b';
    printf("%s", Mystrchr(str,a));
    system("pause");
    return 0;
}
#include
#include

const char *My_strstr(const char *str1, const char *str2)实现strstr
{
    const char *s1 = NULL;
    const char *s2 = NULL;
    const char *start = str1;
    while (*start != '\0')
    {
        s1 = start;//s1回退到上一次的下一个位置
        s2 = str2;//s2回退到str2的起始位置
        while (*s1 != '\0' && *s2 != '\0'&&*s1 == *s2)
        {
            s1++;
            s2++;
        }
        if (*s1 == '\0')
        {
            return NULL;
        }
        if (*s2 == '\0')
        {
            return start;
        }
        start++;
    }
    return NULL;
}

int main()
{
    char dest[20] = "123aabcdef90";
    char src []= "abcdef";
    printf("%s\n", My_strstr(dest, src));
    system("pause");
    return 0;
}
#include
#include

int* my_strcmp(char *dest, const char *src)实现strcmp
{
    assert(dest);
    assert(src);
        while (*dest != '\0'&&*src!='\0')
        {
            if (*dest == *src)
            {
                dest++;
                src++;
            }
            else if (*dest > *src)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        return 0;
}
int main()
{
    char dest[] = "abcdef";
    char src []= "adcdef";
    int ret = my_strcmp(dest, src);
    printf("%d",ret);
    system("pause");
    return 0;
}
#include
#include

void* my_memcpy(void* dest, const void* src, size_t count)
{
    assert(src != nullptr&&dest != nullptr);
    //判断dest指针和src指针是否为空,若为空抛出异常
    char* tmp_dest = (char*)dest;
    const char* tmp_src = (const char*)src;
    //将指针dest和指针src由void强转为char,
    //使得每次均是对内存中的一个字节进行拷贝
    while (count--)
        *tmp_dest++ = *tmp_src++;
    return dest;
}

int main()
{
    char dest[10];
    char* src="abc";
    if(sizeof(dest)
#include
#include

void* my_memmove(void* dest, const void* src, size_t count)
{
    assert(src != nullptr&&dest != nullptr);
    //判断dest指针和src指针是否为空,若为空抛出异常
    char* tmp_dest = (char*)dest;
    const char* tmp_src = (const char*)src;

    if (tmp_src < tmp_dest)//当src地址小于dest地址时,从头进行拷贝
        while (count--)
            *tmp_dest++ = *tmp_src++;
    else if (tmp_src > tmp_dest)//当src地址大于dest地址时,从后进行拷贝
    {
        tmp_src += count - 1;
        tmp_dest += count - 1;
        while (count--)
            *tmp_dest-- = *tmp_src--;
    }
    //else(tmp_src==tmp_dest) 此时不进行任何操作
    return dest;
}

int main()
{
    char dest[10];
    char* src="abc";
    if(sizeof(dest)