fqy131314

实现一个函数,使用指针连接两个字符串。函数输入: 两个源字符串的指针,目的字符串的指(C++指针练习4)

实现一个函数,使用指针连接两个字符串。函数输入: 两个源字符串的指针,目的字符串的指

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

bool str_cat(char *a1,char *a2,char *a3,int len)
{
	int pos=0;

	if(!a3||len<1)
	{
		return false;
	}

	if(a1)
	{
		while(*a1 && pos<(len-1))
		{
			*(a3+pos)=*a1;
			pos++;
			a1++;
		}
	}

	if(a2)
	{
		while(*a2 && pos<(len-1))
		{
			*(a3+pos)=*a2;
			pos++;
			a2++;
		}
	}
	
	*(a3+pos)='\0';
}
int main4(void)
{
	char *a1="我是";
	char *a2="萌小新";
	char a3[64];

	str_cat(a1,a2,a3,64);

	cout<<a3<<endl;

	system("pause");
	return 0;
}

posted on 2022-11-03 10:01  会飞的鱼-blog  阅读(25)  评论(0)    收藏  举报  来源

导航