Given a string, write a function which uses stringstream to remove spaces from the given string
Example
INPUT
s = “I Love C++”
OUTPUT
“ILoveC++”
Algorithm
1. Store the given string in stringstream
2. Now, empty the given string
3. Extract each word from stringstream and concatenate them
C++ Program
#include <bits/stdc++.h> using namespace std; string removeSpaces(string s) { stringstream ss; //to temporarily store each word string temp; //storing the complete string into string stream ss << s; //Making the string empty s = ""; while(!ss.eof()) { //extracting each word from stream ss >> temp; //concatenating s = s + temp; } return s; } int main() { string s = "I Love C++"; cout<<removeSpaces(s)<<endl; }
Try It