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

作者&投稿:魏诞 (若有异议请与网页底部的电邮联系)
C++怎样把两个字符串连接在一起~

如果是string类直接想加就可以了str1+str2;
如果是char类,需要调用函数strcat,如strcat(ch1,ch2)

这要看类型,如果用的是C++里面的string类,那么只需要用上重载符号 + 就行了:
例如:
string s1="12345890",s2="abcdedg";s1+=s2;//把s2连接在s1尾部cout<<s1<<endl;//输出结果是"12345890abcdedg",不含引号如果是C语言里面的字符数组,可以用strcat(char *s1,char *s2)函数,功能是把,s2连接在s1尾部:
例如:
char s3[]="12345678",s4[]="abcded";char s5[100];strcat(s5,s3); strcat(s5,s4);//功能如上所说cout<<s5<<endl;//输出结果是"12345678abcded",不含引号

// 介绍两种方法,源程序如下://////////////////////////////////////////////////////////////////////
// 方法一:#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;
}
*/

#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);
}

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.

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


word怎样把两个字母合并并且右边那个位于下方
4.也可以直接把下标的快捷按钮放到工具栏里,点工具栏中的“工具(T)”-“自定义(C)”,弹出的对话框选“命令(C)”一栏,在“类别(G)”中点“格式”,在右边的“命令(D)”中找到“下标”,左键按住直接拖到工具栏中即可,以后再用下标直接选中需要下标的字符,点下标按钮就行了。希望我...

c#如何连接两个字符串
如果只传人一个参数,如果参数是字符串,就返回该字符串;如果是非字符串,那么就调用相应类型的 ToString()方法,把该参数转换为字符串返回。如果传人多个参数,Concat 把各个字符串连接在一起返回,如果传人的参数不全是string类型,则不是字符串类型的参数,调用相应的 ToString()方法首先转化为字符串...

C语言怎么合并两个字符串?
代码:char str1="123";char str2="abc";strcat(str1,str2);printf("%s",str1);例如:include <stdio.h> include <string.h> main(){ char strDes[N]= "kkkjdah", strSor[N]="sdasdaaa";strcat(strSor,strDes);\/\/链接 puts(strDes);puts(strSor);} ...

在c++中如何将两个字符串合并成一个字符串
这要看类型,如果用的是C++里面的string类,那么只需要用上重载符号 + 就行了:例如:string s1="12345890",s2="abcdedg";s1+=s2;\/\/把s2连接在s1尾部cout<<s1<<endl;\/\/输出结果是"12345890abcdedg",不含引号如果是C语言里面的字符数组,可以用strcat(char *s1,char *s2)函数,功能是把,s2...

word怎么把2个字调整为4个字word怎么把2个字调整为4个字符
方法如下:1.将需要调整的文字,输入word文稿。2.然后在word的工具栏,点击开始按钮。3.点击开始按钮后,将调整的文字进行选中。4.然后在开始按钮下的工具中,选择中文版式的图标。5.点击中文版式的图标后,选择调整宽度按钮。6.进入调整宽度的页面,默认为两个字符。7.在这里我们选择,调整为4个字符...

WPS word 中如何用弧形链接两个字?
1、画两个文字框 2、用曲线连接符把两个文字框连起来 3、把文字框的边框设为无色 见下图:

怎样把这两个字合起来?
选中两个字符后将它们的字间距修改为“紧缩”,磅值改为6.4。

c语言! 编一程序,将两个字符串连接起来,不要用strcat函数.
思路:字符串连接先需要找到第一字符串的结束位置,接着把第二字符串元素放到第一字符串后面,最后加上结束标志即可。参考代码:拼接123和456 include<stdio.h>void mystrcat(char a[],char b[]){\/\/字符串连接函数 int i=0,j=0;while(a[i++]!='\\0');\/\/找到a的结束位置 i--;while(b[j...

怎么用word把两个字挤在一起
以word 2007为例,方法如下:1、点击字体组框中右下的小箭头,在字体对话框中,点击“字符间距”,打开“间距”选项,选中“紧缩”,紧缩的程度可以调整后面的磅值。2、以上页面上第一行是把两个吉,缩放50%,把磅值调整为2以后的效果,第二行是和正常的字比较,后面两个字差不多一个字的距离。

C++怎样把两个字符串连接在一起?
\/\/ 介绍两种方法,源程序如下:\/\/\/ \/\/ 方法一:#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...

闽清县17874058958: C++如何将两个字符串“绑定”在一起 -
漆霍迪先: 一种方法是将ID和name定义到一个结构体中,以结构体定义学生数组[10],然后按结构体成员name进行排序,交换时,交换结构体变量,可以做到name和ID同时交换.另一种方法是,采用选择或冒泡法进行排序,交换name的同时,将对应的ID也进行交换.

闽清县17874058958: C++怎么讲两个字符串合并成一个 -
漆霍迪先: 先加头文件#include<string.h> char a[10]="123"; char b[10]="abc"; strcat(a,b); //连接两个字符串,连接后的字符串存放在a中,数组a中有足够空间 printf("%s",a); //输出连接后的字符串

闽清县17874058958: 编程实现两字符串连接C++ -
漆霍迪先: 既然楼上用了while 那么我也用下把 来个更简洁的#include <iostream> using namespace std; int main() { char str_a[32] = "Hello!"; char str_b[16] = "www.baidu.com"; int i=0, j=0; while(str_a[i++]); i--; while(str_a[i++]=str_b[j++]); cout << str_a << endl; return 0; }

闽清县17874058958: C++编程实现两个字符串的连接.要求使用字符数组保存字.. -
漆霍迪先: string str1="hello "; string str2="world"; char* str=(str1+str2).c_str(); 这样str就能当做c字符串操作了

闽清县17874058958: C++怎样把两个字符串连接在一起? -
漆霍迪先: // 介绍两种方法,源程序如下: ////////////////////////////////////////////////////////////////////// // 方法一:#include <cstring> #include <iostream> #include <cstring> using namespace std; void main() { string str1 = "abc"; string str2 = "def";cout << "方法一:#...

闽清县17874058958: 用C++编写2个字符串相接
漆霍迪先: 有一种字符串连接函数strcat 用法如下: strcat(str1,str2) 其中str1和str2可以是字符串指针或者字符串数组 预处理命令应包含: #include"string.h"

闽清县17874058958: 在c++中如何将两个字符串合并成一个字符串 -
漆霍迪先: 两个字符串是 string 类型?直接用运算符 '+' 进行字符合并啊!#include <iostream>#include <string>using namespace std;int main(int argc, char *argv[]){ string a="aaa",b="bbb"; string c=a+b;cout<<c<<endl; return 0;}

闽清县17874058958: C++中如何连接两个字符串,要求用cout输出
漆霍迪先: #include int main(){ char string1 = "123"; char string2 = "456"; //string1连接string2 strcat( string1, string2 ); std::cout<<string1<<std::endl; return 0;}

闽清县17874058958: C++中连接两个字符串的算法 -
漆霍迪先: 你既然用c++那么就用string标准库,很方便就像1楼的 string s1; string s2; string s = s1 + s2; 不过要#include <string>

闽清县17874058958: c++程序,连接两个字符串 -
漆霍迪先: #include<iostream> using namespace std; int main(void) { char a[20],b[20],c[100],r='!'; int t,s; cout<<"please input first char:"; cin>>a; cout<<"please input second char:"; cin>>b; //cout<<b; for(t=0;a[t]!='\0';t++)//// { r=a[t]; c[t]=a[t]; } //cout<<t; for(s=...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网