模拟实现strcat函数

模拟实现strcat函数

概念:

将两个char类型的字符串连接,中间没有空格。

思路

要连接两个字符串,首先目的空间要足够大,其次,要拷贝到目的空间后面,就是从’\0’
开始拷取,一直拷贝到源的’\0’。

代码

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
	char* start = dest;
	assert(dest != NULL && src != NULL);
	while (*dest != '\0')
		dest++;
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = '\0';
	return start;
}
int main()
{
	char a[20] = "hellow";
	char b[10] = " word !";
	char* c;
	c = my_strcat(a, b);
	printf("%s\n", a);
	system("pause");
	return 0;
}

在这里插入图片描述

发布了29 篇原创文章 · 获赞 12 · 访问量 1134

猜你喜欢

转载自blog.csdn.net/childemao/article/details/90815421