📅  最后修改于: 2020-09-25 06:04:01             🧑  作者: Mango
您可以使用+ 运算符在C++中连接两个字符串对象。
#include
using namespace std;
int main()
{
string s1, s2, result;
cout << "Enter string s1: ";
getline (cin, s1);
cout << "Enter string s2: ";
getline (cin, s2);
result = s1 + s2;
cout << "Resultant String = "<< result;
return 0;
}
输出
Enter string s1: C++ Programming
Enter string s2: is awesome.
Resultant String = C++ Programming is awesome.
您可以使用strcat()
函数在C++中连接两个C样式的字符串 。
#include
#include
using namespace std;
int main()
{
char s1[50], s2[50], result[100];
cout << "Enter string s1: ";
cin.getline(s1, 50);
cout << "Enter string s2: ";
cin.getline(s2, 50);
strcat(s1, s2);
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2;
return 0;
}
输出
Enter string s1: I love
Enter string s2: C++ programming
s1 = I love C++ programming
s2 = C++ programming