#include <iostream>
#include <string>
using namespace std;
int main()
{
int i=0;
string s;
getline(cin, s);
while (s[i])
{
if (s[i]==' ' && s[i+1]==' '){
s.erase(i,1); i--;
}
i++;
}
if (s[0]==' '){
s.erase(0,1);
}
if (s[s.length() - 1]==' '){
s.erase(s.length() - 1, s.length() - 1);
}
cout << s << endl;
return 0;
}
Объяснение: