CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]) {
//define a string with extra spaces
string s(" aaaaa mmmm "), s1;
cout<<s<<","<<'\n';
//make a stream from the string
stringstream ss(s);
//parse the words while skipping spaces
//re-assemble the words with only 1 space
string word;
ss >> s1; //parse first word;
while (ss >> word) { //parse remaining words
s1 += ' ' + word;
}
//show result
cout<<","<<s1<<","<<endl;
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run