//编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串
#include<iostream>
#include<string>
using namespace std;
int main()
{
char cont='y' ;
string s, result ;
//提示用户输入第一个字符串
cout << " 请输入第一个字符串: " << endl;
//判断第一个字符串是否正确
while (cin >> s)
{
result += s;
//提示用户是否继续输入字符串
cout << "是否继续(y or n )? " << endl;
cin >> cont;
//判断用户是否继续输入字符串
if (cont == 'y' || cont == 'Y') //切记:单引号内不能包含空格
//提示用户继续输入下一个字符串
cout << " 请输入下一个字符串: " << endl;
else
break;
}
cout << "拼接后的字符串是: " << result << endl;
system("pause");
return 0;
}
//修改上面的程序,用空格把输入的多个字符串分隔开来
#include<iostream>
#include<string>
using namespace std;
int main( )
{
char cont = 'y';
string s, result;
//提示用户输入第一字符串
cout << "请输入第一个字符串: " << endl;
while (cin >> s)
{
//第一个拼接的字符串之前不加空格
if (!result.size( ))
result += s;
else //之后拼接的每个字符串之前加一个空格
result = result + " " + s;
//提示用户是否继续输入
cout << " 是否继续(y or n )?" << endl;
cin >> cont;
//判断是否继续输入
if (cont == 'y' || cont == 'Y')
cout << "请输入下一个字符串: " << endl;
else
break;
}
cout << "拼接的字符串是:" <<result<< endl;
system("pause");
return 0;
}
<C++ Primer_5th>习题_3.5
最新推荐文章于 2021-09-23 17:18:31 发布
