c++程序设计第3版谭浩强课后答案
C++编程:编写一个程序,将两个字符串连接起来,结果取代第一个字符串。
1. 用字符数组,不用strcat函数(即自己写一个具有strcat函数功能的函数);
2. 用标准库中的strcat函数;
3. 用string 方法定义字符串变量。
以下是此题的【c++源代码】,需要【c源代码】请点击进入
1. 用字符数组,不用strcat函数(即自己写一个具有strcat函数功能的函数);
#include <iostream>
#include <string>
using namespace std;
int main()
{char s1[80],s2[40];
int i=0,j=0;
cout<<"input string1:";
cin>>s1;
cout<<"input string2:";
cin>>s2;
while (s1[i]!='\0')
i++;
while(s2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
cout<<"The new string is:"<<s1<<endl;
return 0;
}
2. 用标准库中的strcat函数;
include <iostream>
using namespace std;
int main()
{char s1[80],s2[40];
cout<<"input string1:";
cin>>s1;
cout<<"input string2:";
cin>>s2;
strcat(s1,s2);
cout<<"The new string is:"<<s1<<endl;
return 0;
}
3. 用string 方法定义字符串变量。
#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="week",s2="end";
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
s1=s1+s2;
cout<<"The new string is:"<<s1<<endl;
return 0;
}
本文链接:http://www.wb98.com/cjia/post/cjia_5.13.html
本站文章搜索: