模拟实现库函数strcpy、strlen、strcat、strstr、strchr、strcmp、memcpy、memmove

库函数strcpy的模拟实现:把从src地址开始且含有’\0’结束符的字符串复制到以dest开始的地址空间,返回值的类型为char*

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char * my_strcpy(char *dest, const char *src)
{
    char *ret = dest;
    assert(ret != NULL);
    assert(src != NULL);
    while (*ret++ = *src++)
        ;
    return dest;
}
int main()
{
    char arr[] = "123456";
    printf("%s\n", my_strcpy(arr,"abcdef"));
    system("pause");
    return 0;
}


里面有4 点注意事项:

  • 返回类型 char *,实现链式访问。

  • const修饰的源指针src,具有常属性,不会被更改。

  • assert断言,保证健壮性、鲁棒性。

  • while循环自带指针++,优化代码。

库函数strlen的模拟实现:计算给定字符串的长度

1.创建计数变量

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int my_strlen(const char *src)
{
    int count=0;
    assert(src != NULL);
    while (*(src++))
        count++;
    return count;
}
int main()
{
    printf("%d\n", my_strlen("abcdef"));
    system("pause");
    return 0;
}

2.使用递归

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int my_strlen(const char *str)
{
    assert(str != NULL);
    if (*str == '\0')
        return 0;
    else
        return 1 + my_strlen(str + 1);
}
int main()
{
    printf("%d\n", my_strlen("abcdef"));
    system("pause");
    return 0;
}


3 使用指针-指针的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int my_strlen(const char *str)
{
    const char *p = str;
    assert(str != NULL);
    while (*p != '\0')
    {
        p++;
    }
    return p - str;
}
int main()
{
    printf("%d\n", my_strlen("abcdef"));
    system("pause");
    return 0;
}

库函数strcat的模拟实现:字符串连接函数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *my_strcat(char *dest, const char *src)
{
    char *ret = dest;
    assert(dest != NULL);
    assert(src != NULL);
    while (*dest)
        dest++;
    while (*dest++ = *src++)
        ;
    return ret;
}
int main()
{
    char arr[20] = "hello ";
    printf("%s\n", my_strcat(arr, "friend"));
    system("pause");
    return 0;
}

库函数strstr的模拟实现:从字符串str1中查找是否有字符串str2,如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回NULL

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
char *my_strstr(const char *dest, const char *src)
{
    const char *s1 = dest;
    const char *s2 = src;
    assert(dest && src != NULL);
    if (*s2 == '\0')
        return (char*)dest;
    while (*dest != '\0')
    {
        s1 = dest;
        s2 = src;
        while (*s1 && *s2 && *s1 == *s2)
        {
            s1++;
            s2++;
        }
        if (*s2 == '\0')
        {
            return (char*)dest;
        }
        dest++;
    }
    return NULL;
}
int main()
{
    char arr1[] = "abbbbcdef";
    char arr2[] = "bbc";
    char * ret =my_strstr(arr1, arr2);
    if (ret != NULL)
        printf("%s\n", ret);
    else
        printf("没找到\n");
    system("pause");
    return 0;
}

库函数strchr的模拟实现:返回首次出现_Val的位置的指针,返回的地址是被查找字符串指针开始的第一个与Val相同字符的指针,如果Str中不存在Val则返回NULL

char *my_strchr(const char *p, int chr)
{
    const char *s = p;
    assert(p);
    while (*s != (char)chr && *s)
        s++;
    if (*s == (char)chr)
        return (char *)s;
    return NULL;
}
int main()
{
    char *p = "abcdef";
    char chr = 'c';
    char *ret = my_strchr(p, chr);
    if (ret != NULL)
        printf("找到了,%s\n", ret);
    else
        printf("没找到\n");
    system("pause");
    return 0;
}

库函数strcmp的模拟实现:比较两个字符串。设这两个字符串为str1,str2,若str1==str2,则返回零 ;若str1 < str2,则返回负数; 若str1 > str2,则返回正数。

方法1:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int my_strcmp(const char *dest, const char *src)
{
    assert(dest&&src != NULL);
    while (*dest == *src && *src)
    {
        dest++;
        src++;  
    }
    return *dest - *src;
}
int main()
{
    char arr1[] = "abcdef";
    char arr2[] = "aaaaaaa";
    int ret = my_strcmp(arr1, arr2);
    printf("ret=%d\n", ret);
    system("pause");
    return 0;
}


方法2:(库函数版)

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

我们发现strcpy是不能拷贝内容为0的字符串的内容,所以我们引入了memcpy

库函数memcpy的模拟实现:内存拷贝函数,从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
void *my_memcpy(void *dest, const void *src, int count)
{
    void *cp = dest;
    assert(dest && src);
    while (count--)
    {
        *(char *)dest = *(char *)src;
        ++(char *)dest;
        ++(char *)src;
    }
    return cp;
}

int main()
{
    int arr1[5] = { 1, 2, 3, 4, 5 };
    int arr2[5] = { 6, 7, 8, 9, 0 };
    int i = 0;
    my_memcpy(arr1, arr2, 20);
    for (i = 0; i < 5;i++)
        printf("%d ",arr1[i]);
    printf("\n");
    system("pause");
    return 0;
}

库函数memmove的模拟实现:用于从src拷贝count个字节到dest,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中。但复制后src内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同

void *my_memmove(void *dest, const void *src, int count)
{
    void *cp = dest;
    assert(dest&&src);
    if (dest < src)
    {
        while (count--)
        {
            *(char *)dest = *(char *)src;
            ++(char *)dest;
            ++(char *)src;
        }
    }
    else
    {
        while (count--)
        {
            *((char *)dest + count) = 
              *((char *)src + count);
        }
    }
    return cp;
}
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int i = 0;
    int sz = sizeof(arr) / sizeof(arr[0]);
    my_memmove(arr, arr+2, 16);
    for (i = 0; i < sz; i++)
        printf("%d ", arr[i]);
    printf("\n");
    system("pause");
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值