模拟实现拷贝字符串

1.

#include <stdio.h>
#include <assert.h>
void my_strcpy(char* dest, char* src)
{
	assert(dest, src);
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;
}
int main()
{
	char arr1[20] = { 0 };
	char arr2[] = "hello world";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

模拟实现拷贝字符串

2.

#include <stdio.h>
#include <assert.h>
void my_strcpy(char* dest, char* src)
{
	assert(dest && src);
	while (*dest++ = *src++)
	{
		;
	}
}
int main()
{
	char arr1[20] = { 0 };
	char arr2[] = "hello world";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

 模拟实现拷贝字符串

3.

#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
		;
	return ret;
}
int main()
{
	char arr1[20] = { 0 };
	char arr2[] = "hello world";
	printf("%s\n", my_strcpy(arr1, arr2));
	return 0;
}

 模拟实现拷贝字符串

 

上一篇:数组知识


下一篇:10种方式实现数组的去重