strcat()链接函数

My_strcat()链接函数

#include "stdafx.h"
#include<iostream>
#include "assert.h"
using namespace std;
char *my_strcat(char *str2, const char *str1){
	char *p =str2;
	assert(str1 != NULL || NULL != str2);//assert断言库函数,满足条件终止整个程序
	while (*p != '\0')++p;               //让p指向str2第一个'\0'位置
	while ((*p++ = *str1++)!='\0');      //把str1复制给str1后面的空间
	return str2;
}
int main()
{
	char ch1[20] = { "student" };
	char ch2[20] = { "a " };
	char ch3[30] = {"I am "};
    my_strcat(ch2, ch1);
	my_strcat(ch3, my_strcat(ch2, ch1));
	cout << ch1 << endl << ch2 << endl << ch3<<endl;
	return 0;
}

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43886592/article/details/85255394