39.已知有如下string对象:
string line1 = “ We were her pride of 10 she named us:“;
string line2 = “Benjamin, Phoenix, the Prodigal“;
string line3 = “and perspicacious pacific Suzanne“;
string sentence = line1 + ‘ ‘ + line2 + ‘ ‘ + line3;
编写程序计算sentence中有多少个单词,并指出其中最长和最短单词。如果有多个最长或最短单词,则将它们全部输出。
没看提示的时候这么写的。。能输出,但是很繁琐。。而且不知道istringstream如何重新绑定。。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include<iostream> #include<vector> #include<sstream> #include<fstream> #include<cstdlib> #include<list> #include<cstring> #include<string> #include<deque> using namespace std; int main() { string str2="wo ai nimen"; string str3="ni men xingbuxing"; string str4="buzhidao xingbu xing a"; string line=str2+str3+str4; string::size_type pos=0; string str=" "; istringstream sin(line); string word; int num2=1; int num3=1; while(sin>>word) { cout<<word<<" "<<word.size()<<endl; if(word.size()>num2)num2=word.size(); if(word.size()<num3)num3=word.size(); } istringstream sin2(line); while(sin2>>word) { if(word.size()==num2)cout<<word<<" "; } istringstream sin3(line); while(sin3>>word) {if(word.size()==num3)cout<<word<<" ";} return 0; }
41.已知有如下string对象:
string generic1(“Dear Ms Daisy:”);
string generic2(“MrsMsMissPeople”);
编写程序实现下面函数:
string greet( string form, string lastname, string title, string::size_type pos, int length );
该函数使用replace操作实现以下功能:对于字符串form,将其中的Daisy替换为lastname,将其中的Ms替换为字符串generic2中从pos下标开始的length个字符。
例如,下面的语句:
string lastName( “ AnnaP” );
string salute = greet( generic1, lastName, generic2, 5, 4 );
将返回字符串:Dear Miss AnnaP:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include<iostream> #include<vector> #include<sstream> #include<fstream> #include<cstdlib> #include<list> #include<cstring> #include<string> #include<deque> using namespace std; string greet(string form, string lastname, string title, string::size_type pos, int length ) { form.replace(8,4,lastname); form.replace(5,2,title,pos,length); return form; } int main() { string str2="Dear Ms Dass"; string str3="MrsMsMissPeople"; string lastName="AnnaP"; cout<<greet(str2,lastName,str3,5,4)<<endl; return 0; }
最后
以上就是震动毛巾最近收集整理的关于9-39的全部内容,更多相关9-39内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复