#include<iostream>
using namespace std;
int main(){
int flag = 0;
string s1, s2;
getline(cin, s1);
for(int i = 0; s1[i]; ++i){
if(s1[i] == ' ' && flag == 0){
flag = 1;
s2 = s2 + s1[i];
}
else if(s1[i] != ' ' && flag == 1){
s2 = s2 + s1[i];
flag = 0;
}
else if(s1[i] != ' ')
s2 = s2 + s1[i];
}
cout << s2 << endl;
return 0;
}
用cin或者scanf自动过滤空格
#include<iostream>
using namespace std;
int main(){
string s;
while(cin >> s)
cout << s << " ";
return 0;
}