Skip to content
Related Articles

Related Articles

Removing spaces from a string using Stringstream
  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2020


Solution to removing spaces from a string is already posted here. In this article another solution using stringstream is discussed.

Algorithm

1. Enter the whole string into stringstream.
2. Empty the string.
3. Extract word by word and concatenate to the string.

Program 1: Using EOF.




// C++ program to remove spaces using stringstream
#include <bits/stdc++.h>
using namespace std;
  
// Function to remove spaces
string removeSpaces(string str)
{
    stringstream ss;
    string temp;
  
    // Storing the whole string
    // into string stream
    ss << str;
  
    // Making the string empty
    str = "";
  
    // Running loop till end of stream
    while (!ss.eof()) {
  
        // Extracting word by word from stream
        ss >> temp;
  
        // Concatenating in the string to be
        // returned
        str = str + temp;
    }
    return str;
}
  
// Driver function
int main()
{
    // Sample Inputs
    string s = "This is a test";
    cout << removeSpaces(s) << endl;
  
    s = "geeks for geeks";
    cout << removeSpaces(s) << endl;
  
    s = "geeks quiz is awsome!";
    cout << removeSpaces(s) << endl;
  
    s = "I love     to     code";
    cout << removeSpaces(s) << endl;
  
    return 0;
}


Output:

Thisisatest
geeksforgeeks
geeksquizisawsome!
Ilovetocode

Program 2: Using getline().






// C++ program to remove spaces using stringstream
// and getline()
#include <bits/stdc++.h>
using namespace std;
  
// Function to remove spaces
string removeSpaces(string str)
{
    // Storing the whole string
    // into string stream
    stringstream ss(str);
    string temp;
  
    // Making the string empty
    str = "";
  
    // Running loop till end of stream
    // and getting every word
    while (getline(ss, temp, ' ')) {
        // Concatenating in the string
        // to be returned
        str = str + temp;
    }
    return str;
}
// Driver function
int main()
{
    // Sample Inputs
    string s = "This is a test";
    cout << removeSpaces(s) << endl;
  
    s = "geeks for geeks";
    cout << removeSpaces(s) << endl;
  
    s = "geeks quiz is awsome!";
    cout << removeSpaces(s) << endl;
  
    s = "I   love     to     code";
    cout << removeSpaces(s) << endl;
  
    return 0;
}
  
// Code contributed by saychakr13


Output:

Thisisatest
geeksforgeeks
geeksquizisawsome!
Ilovetocode

This article is contributed by Nishant. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :