【C/C++】【练习】常用库函数实现

memcpy

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void* my_memcpy(void* dst, const void* src, size_t size)
{
    assert( (dst != NULL) && (src != NULL));

    char* pdst = (char*) dst;
    const char* psrc = (const char*) src;

    //如果内存重叠,从后往前拷贝
    if(pdst < psrc && pdst < psrc + size)
    {
        pdst = pdst + size - 1;
        psrc = psrc + size - 1;
        while(size--)
            *pdst-- = *psrc--;
    }
    else
    {
        while(size--)
            *pdst++ = *psrc++;
    }
    return dst;
}

int main()
{
    int arr[10];
    for(int i = 0; i < 10; i++)
        arr[i] = i;

    for(int i = 0; i < 10; i++)
        cout << arr[i] << " ";
    cout << endl;
    my_memcpy(arr + 4, arr, sizeof(int) * 6);
    for(int i = 0; i < 10; i++)
        cout << arr[i] << " ";
    cout << endl;
    return 0;
}

//memcpy
//0 1 2 3 4 5 6 7 8 9
//0 1 2 3 0 1 2 3 4 5

//0 1 2 3 4 5 6 7 8 9
//0 1 2 3 0 1 2 3 0 1

memset

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void* my_memset(void* src, int c, size_t size)
{
    assert(src != NULL);
    char* psrc = (char*)src;
    while(size--)
        *psrc++ = (char)c;
    return src;
}

int main()
{
    int arr[10];
    for(int i = 0; i < 10; i++)
        arr[i] = i;

    for(int i = 0; i < 10; i++)
        cout << arr[i] << " ";
    cout << endl;
    my_memset(arr, 0, sizeof(int) * 10);
    for(int i = 0; i < 10; i++)
        cout << arr[i] << " ";
    cout << endl;
    return 0;
}


strcpy

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void* my_strcpy(char* dst, char* src)
{
    assert(dst != NULL && src != NULL);
    char* ret = dst;
    int size = strlen(src) + 1;
    if(dst >= src && dst < src + size)
    {
        dst = dst + size - 1;
        src = src + size - 1;
        while(size--)
            *dst-- = *src--;
    }
    else
    {
        while(size--)
            *dst++ = *src++;
    }
    return ret;
}

int main()
{
    char dst[] = "hello world";
    char src[] = "ok";
    my_strcpy(dst, src);

    printf("%s\n", dst);
    printf("%s\n", src);

    return 0;
}


strncpy

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void* my_strncpy(char* dst, char* src, size_t size)
{
    assert(dst != NULL && src != NULL);
    if(dst > src && dst < src + size)
    {
        int len = strlen(src) < size ? strlen(src) : size;
        char* pdst = dst + len;
        *pdst-- = '\0';
        src = src + len - 1;
        while(len--)
            *pdst-- = *src--;
    }
    else
    {
        char* pdst = dst;
        while(size--)
            *pdst++ = *src++;
        if(*(pdst - 1) != '\0') *pdst = '\0';
    }
    return dst;
}

int main()
{
    char dst[] = "hello world";
    char src[] = "ok";
    my_strncpy(dst, src, 1);

    printf("%s\n", dst);
    printf("%s\n", src);

    return 0;
}


strcat

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

void* my_strcat(char* dst, const char* src)
{
    assert(dst != NULL && src != NULL);
    char* pdst = dst;
    while(*pdst != '\0')
        pdst++;
    while((*pdst++ = *src++) != '\0');
    return dst;
}

int main()
{
    char dst[] = "hello world";
    char src[] = " ok";
    printf("%s\n", dst);
    printf("%s\n", src);
    my_strcat(dst, src);
    printf("%s\n", dst);
    printf("%s\n", src);
    return 0;
}


strcmp

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

int my_strcmp(const char* str1, const char* str2)
{
    assert(str1 != NULL && str2 != NULL);
    while(str1 && str2 && *str1 == *str2)
    {
        str1++;
        str2++;
    }
    return *str1 - *str2;
}

int main()
{
    char dst[] = "hello world";
    char src[] = " ok";
    cout << my_strcmp(dst, src) << endl;
    return 0;
}

strlen

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

size_t my_strlen(const char* str)
{
    assert(str != NULL);
    size_t ret = 0;
    while(*str++ != '\0')
        ret++;
    return ret;
}

int main()
{
    char dst[] = "hello world";
    char src[] = " ok";
    cout << my_strlen(dst) << endl;
    cout << my_strlen(src) << endl;
    return 0;
}
posted @ 2020-08-03 22:07  NaughtyCoder  阅读(167)  评论(0)    收藏  举报