The Wayback Machine - https://web.archive.org/web/20240118184214/https://www.geeksforgeeks.org/removing-spaces-string-using-stringstream/
Open In App
Related Articles

Removing spaces from a string using Stringstream

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 

CPP




// 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 awesome!";
    cout << removeSpaces(s) << endl;
 
    s = "I love     to     code";
    cout << removeSpaces(s) << endl;
 
    return 0;
}


Output

Thisisatest
geeksforgeeks
geeksquizisawesome!
Ilovetocode

Time complexity: O(n) where n is length of string
Auxiliary space: O(n)

Program 2: Using getline(). 

CPP




// 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 awesome!";
    cout << removeSpaces(s) << endl;
 
    s = "I   love     to     code";
    cout << removeSpaces(s) << endl;
 
    return 0;
}
 
// Code contributed by saychakr13


Output

Thisisatest
geeksforgeeks
geeksquizisawesome!
Ilovetocode

Time complexity: O(n) 
Auxiliary Space: O(n)


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 30 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads