本来一个很简单的字符串拼接问题,因为题目要在2个字符串之间加逗号和空格,感觉很烦了,
我的方法代码看起来很丑,有没有更好方法,谢谢!
#include<iostream>
#include<string>
int main()
{
using namespace std;
string s1,s2,s3;
char t[3]={',','\0'};
cout << "Enter your first name: ";
cin >> s1;
cout << "Enter yor last name: ";
cin >> s2;
s3=s1+t;
cout << "Here's the information in a single string: " << s3 << ' ' << s2 <<endl;
return 0;
}
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
const int n=20;
char first[n],last[n];
cout << "Enter your first name: ";
cin.getline(first,n);
cout << "Enter your last name: ";
cin.getline(last,n);
char t[2]={',','\0'};
cout << "Here's the information in a single string: " << strcat(first,t) << ' ' << last <<endl;
return 0;
}