编写一程序,将两个字符串连接起来的3种方法
2013-07-20 23:09
281 查看
1.用字符数组和自己书写的函数实现
自己写一个具有strcat函数功能的函数实现代码如下:
#include<iostream> using namespace std; int main(){ char a[100],b[50]; void Strcat(char a[],char b[]); cout<<"please input first string:"<<endl; cin>>a; cout<<"please input second string:"<<endl; cin>>b; Strcat(a,b); cout<<"The new string: "<<a; cout<<endl; return 0; } void Strcat(char a[],char b[]){ int i,j; for(i=0;a[i]!='\0';i++); cout<<"Length of first string:"<<i<<endl; for(j=0;b[j]!='\0';j++,i++){ a[i]=b[j]; } cout<<"Length of second string:"<<j<<endl; }
2.用标准库中的strcat函数
使用strlen()函数求数组的大小,strcat()函数用来连接字符串实现代码如下:
#include<iostream> #include<string> using namespace std; int main(){ char a[100],b[50]; cout<<"please input first string:"<<endl; cin>>a; cout<<"please input second string:"<<endl; cin>>b; cout<<"Length of first string :"<<strlen(a)<<endl; cout<<"Length of first string :"<<strlen(b)<<endl; cout<<"The new string: "<<strcat(a,b); cout<<endl; return 0; }
3.用string方法定义字符串变量
#include<iostream> #include<string> using namespace std; int main(){ string a,b; cout<<"please input first string:"<<endl; cin>>a; cout<<"please input second string:"<<endl; cin>>b; cout<<"New string :"<<(a+b)<<endl; return 0; }
相关文章推荐
- 编写一程序,将两个字符串连接起来的3种方法
- 编写一个程序,将两个字符串链接起来,结果取代第一个字符串。要求用方法
- 7.13 编写一个程序,将两个字符串连接起来,不要用strcat函数。
- 实验5-7 编写程序,将两个字符串连接起来,不要用strcat函数。
- 编一个程序,将两个字符串连接起来,不要用strcat函数。
- 编一程序,将两个字符串连接起来,不要用strcat函数。
- 习题 6.13 编一程序,将两个字符串连接起来,不要用strcat函数。
- 在Matlab中,想要将两个字符串连接在一起,有以下的方法: 假定有两个字符串 >> str1='Iloveyou';str2='123'; 方法一:用中括号将str1和str2像矩阵元素一样包含起来
- 编一程序,将两个字符串连接起来,即实现strcat函数功能
- 编写一段程序,从标准输入中读取多个字串并将他么连接起来,输出成大的字符串。 改写上述程序,用空格把输入的多个字符串分割开来。
- 编写一个程序,将两个字符串链接起来, 结果取代第一个字符串
- 将两个字符串连接起来取代第一个字符串。用string方法
- 输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序(递归方法)
- 1.编写一个名称为MyClass一个类,在该类中编写一个方法,名称为CountChar,返回值为整型,参数两个,第一个参数可以是字符串、整数、单精度、双精度,第二个参数为字符,方法功能返回第二个参数在
- 黑马程序员_编写程序,获取两个字符串中最大相同子串
- 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。
- 将两个字符串连接起来 用string
- 编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根 据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。
- 在C#中把两个DataTable连接起来,相当于Sql的Inner Join方法
- 编写程序,获取两个字符串中最大相同子串