模拟实现库函数strcat

【函数作用】:strcat的作用是连接两个字符串,l例如chars1[]="hello",chars2[]="world",strcat(s1,s2)的结果就是"helloworld",中间没有空格.

【函数原型】: 我们在MSDN里看一下strcat函数的原型


【模拟实现】:
 
 
#include<stdio.h>
#include<assert.h>

char *my_strcat(char *dest, const char *sour)
{
	char *ret = dest;
	assert(dest != NULL);
	assert(sour != NULL);//断言
	while (*dest)
	{
		dest++;
	}//找到目标字符串的'\0',从'\0'开始copy
	while (*dest++ = *sour++)
	{
		;
	}//strcpy函数
	return ret;
}
int main()
{
	char s1[] = "hello ";
	char s2[] = "world";
	char *ret=my_strcat(s1, s2);
	printf("%s\n", ret);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/hansionz/article/details/80310961