chc2203 发表于 2014-12-22 17:39:50

C/C++ 连接两个字符串

上网查了很多相关的资料,结果是大同小异,有的看不懂,有的太生硬!

一: C风格字符串连接
#include <iostream>
using namespace std;

int main()
{
const char *str = "hello ";
const char *str2 = "world";
const size_t len = strlen(str)+strlen(str2);
char *n_str = new char;
strcpy(n_str,str);
strcat(n_str,str2);
cout<<n_str<<endl;
delete [] n_str;
return 0;
}

二|:C++ string类型字符串
#include <iostream>
#include <string>
using namespace std;

int main()
{
const string str="hello ";
const string str2="world";
string n_str;
n_str = str;
n_str +=str2;
cout<<n_str<<endl;
return 0;
}
页: [1]
查看完整版本: C/C++ 连接两个字符串