C++怎样把两个字符串连接在一起?

用字符串函数,要注释!
2024-12-31 19:32:19
推荐回答(4个)
回答1:

// 介绍两种方法,源程序如下://////////////////////////////////////////////////////////////////////
// 方法一:#include #include
#include
using namespace std;void main()
{
string str1 = "abc";
string str2 = "def"; cout << "方法一:#include" << endl;
cout << "连接前:" << endl;
cout << "str1: " << str1.data() << endl;
cout << "str2: " << str2.data() << endl; str1.append(str2);

cout << endl;
cout << "连接后:" << endl;
cout << "str1: " << str1.data() << endl;
cout << "str2: " << str2.data() << endl;
}/////////////////////////////////////////////////////////////////////
// 方法二:#include
/*
#include
#include void main()
{
char str1[10] = "abc", str2[] = "def"; cout << "方法二:#include " << endl;
cout << "连接前:" << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl; strcat(str1,str2); cout << endl;
cout << "连接后:" << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
}
*/

回答2:

#include
void main()
{char s1[80],s2[40];
int i=0,j=0;
printf("\ninput string1:");
scanf("%s",s1);
printf("input string2:");
scanf("%s",s2);
while(s1[i]!='\0')
i++;
while(s2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
printf("The new string is:%s\n",s1);
}

回答3:

char *strcat( char *strDestination, const char *strSource );wchar_t *wcscat( wchar_t *strDestination, const wchar_t *strSource );ParametersstrDestination Null-terminated destination string strSource Null-terminated source string LibrariesAll versions of the C run-time libraries.Return ValueEach of these functions returns the destination string (strDestination). No return value is reserved to indicate an error.RemarksThe strcat function appends strSource to strDestination and terminates the resulting string with a null character. The initial character of strSource overwrites the terminating null character of strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.wcscat is the wide-character version of strcat. The arguments and return value of wcscat are wide-character strings. These two functions behave identically otherwise.

回答4:

你好,比如下边这个#include #include using namespace std;void main(){ char a[50],b[50]; cin>>a>>b; cout<