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

用字符串函数,要注释!

第1个回答  2013-06-10
// 介绍两种方法,源程序如下://////////////////////////////////////////////////////////////////////
// 方法一:#include <cstring>#include <iostream>
#include <cstring>
using namespace std;void main()
{
string str1 = "abc";
string str2 = "def"; cout << "方法一:#include<cstring>" << 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 <string.h>
/*
#include <iostream.h>
#include <string.h>void main()
{
char str1[10] = "abc", str2[] = "def"; cout << "方法二:#include <string.h>" << 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个回答  推荐于2016-04-02
#include<stdio.h>
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个回答  2013-06-10
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个回答  2013-06-10
你好,比如下边这个#include <string>#include <iostream>using namespace std;void main(){ char a[50],b[50]; cin>>a>>b; cout<<strcat(a,b)<<endl;; }