c++连续输入字符串并拼接,以空格隔开输出
最后一个字符串后面不需要空格。思路:使用while(cin>>str)连续输入。在每个字符串的前面加空格,这样就保证最后一个字符串没有空格。但是需要判断一下第一个字符串是否为空,为空就前面不需要加空格。#include<iostream>#include<string>using namespace std;int main() {string stmp, sr
·
最后一个字符串后面不需要空格。
思路:使用while(cin>>str)连续输入。在每个字符串的前面加空格,这样就保证最后一个字符串没有空格。但是需要判断一下第一个字符串是否为空,为空就前面不需要加空格。
#include<iostream>
#include<string>
using namespace std;
int main() {
string stmp, sresult;
cout << "input a string(ctrl+z) to end process" << endl;
while (cin >> stmp) {
if (sresult.empty() == true) {
sresult = stmp;
}
else {
sresult += (" " + stmp);
}
}
cout << "your inputs are: " << sresult << endl;
return 0;
}
更多推荐
已为社区贡献5条内容
所有评论(0)