其他分享
首页 > 其他分享> > 模拟实现拷贝字符串

模拟实现拷贝字符串

作者:互联网

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;
}

 

 

标签:src,dest,char,strcpy,arr1,字符串,拷贝,include,模拟
来源: https://blog.csdn.net/m0_62513305/article/details/121505523