库函数的模拟实现

发布时间:2024年01月24日

目录

模拟实现strlen

模拟实现strcpy

模拟实现strcmp

模拟实现strcat

模拟实现strstr

模拟实现memcpy

模拟实现memmove


模拟实现strlen

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int MyStrlen(char* str)
{
	int len = 0;

	while (str[len] != '\0')
		len++;

	return len;
}

int main()
{
	char str[50] = { "Hello,world!" };

	int ans = MyStrlen(str);

	printf("%d\n", ans);

	return 0;
}

模拟实现strcpy

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

char* MyStrcpy(char* str2, char* str1)
{
	char* p = str2;

	while (*p++ = *str1++)
		;

	return str2;
}

int main()
{
	char str1[50] = { "Hello,world!" };
	char str2[50];

	MyStrcpy(str2, str1);

	printf("%s\n", str2);

	return 0;
}

模拟实现strcmp

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int MyStrcmp(const char* str1, const char* str2)
{
	int i = 0;
	while (str1[i] != '\0' && str2[i] != '\0' && str1[i] == str2[i])
		i++;

	return str1[i] - str2[i];
}

int main()
{
	const char* str1 = "apple";
	const char* str2 = "banana";
	const char* str3 = "apple";

	int ans1 = MyStrcmp(str1, str2);
	int ans2 = MyStrcmp(str1, str3);

	printf("%d\n%d", ans1, ans2);

	return 0;
}

模拟实现strcat

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

char* MyStrcat(char* str1, const char* str2)
{
	char* re = str1;
	while (*str1)
		str1++;

	while (*str2)
	{
		*str1 = *str2;
		str1++;
		str2++;
	}

	*str1 = '\0';

	return re;
}

int main()
{
	char str1[50] = "Hello,";
	const char* str2 = "world!";

	printf("%s\n", MyStrcat(str1, str2));

	return 0;
}

模拟实现strstr

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

char* MyStrstr(const char* str1, const char* str2)
{
	if (*str2 == '\0')
		return (char*)str1;

	while (*str1)
	{
		const char* s1 = str1;
		const char* s2 = str2;

		while (*s1 && *s2 && (*s1 == *s2))
		{
			s1++;
			s2++;
		}

		if (*s2 == '\0')
			return (char*)str1;

		str1++;
	}

	return NULL;
}

int main()
{
	const char* str1 = "Hello world";
	const char* str2 = "world";

	char* ans = MyStrstr(str1, str2);

	if (ans)
		printf("%ld\n", ans - str1);
	else
		printf("没找到\n");

	return 0;
}

模拟实现memcpy

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

void* MyMemcpy(void* brr, const void* arr, size_t size)
{
	char* b = (char*)brr;
	const char* a = (const char*)arr;

	for (size_t i = 0; i < size; i++)
		b[i] = a[i];

	return brr;
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5 };
	int brr[5] = { 0 };

	MyMemcpy(brr, arr, sizeof(brr));

	int size = sizeof(brr) / sizeof(brr[0]);

	for (int i = 0; i < size; i++)
	{
		printf("%d ", brr[i]);
	}

	return 0;
}

模拟实现memmove

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include <cassert>

void* MyMemmove(void* dest, const void* src, size_t size)
{
	assert(dest && src);
	char* ret = (char*)dest;
	if (dest < src)
	{
		while (size--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else if (dest > src)
	{
		while (size--)
		{
			*((char*)dest + size) = *((char*)src + size);
		}
	}
	return ret;
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 };

	MyMemmove(arr + 3, arr, 20);

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}

感谢大佬支持!!!

文章来源:https://blog.csdn.net/weixin_74809706/article/details/135826509
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。