信息发布→ 登录 注册 退出

c++ 连接两个字符串实现代码 实现类似strcat功能

发布时间:2026-01-11

点击量:
复制代码 代码如下:
#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char s1[60]="kingbaby";
char *s2="hello";
int i=0;int j=0;
while(s1[i]!='\0')i++;
while((s1[i]=s2[j])!='\0'){
j++;i++;
}
cout<<s1<<endl;
return 0;
}

方法二
复制代码 代码如下:
#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char a[20] ="aaaa";
char b[10]="bbb";
char *stra=a;
char *strb=b;

while(*stra!='\0')stra++;
while(*strb!='\0')
{
*stra=*strb;//同时移动指针
strb++;
stra++;
}
return 0;
}
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!