【海贼王编程冒险 - C语言海上篇】库函数怎样模拟实现?

2024-06-04 11:31:12 浏览数 (2)

模拟实现strlen

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

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

	while (str[len] != '')
		len  ;

	return len;
}

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

	int ans = MyStrlen(str);

	printf("%dn", ans);

	return 0;
}

模拟实现strcpy

代码语言:javascript复制
#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("%sn", str2);

	return 0;
}

模拟实现strcmp

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int MyStrcmp(const char* str1, const char* str2)
{
	int i = 0;
	while (str1[i] != '' && str2[i] != '' && 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("%dn%d", ans1, ans2);

	return 0;
}

模拟实现strcat

代码语言:javascript复制
#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 = '';

	return re;
}

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

	printf("%sn", MyStrcat(str1, str2));

	return 0;
}

模拟实现strstr

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

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

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

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

		if (*s2 == '')
			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("%ldn", ans - str1);
	else
		printf("没找到n");

	return 0;
}

模拟实现memcpy

代码语言:javascript复制
#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

代码语言:javascript复制
#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;
}

感谢大佬支持!!!

0 人点赞