Removing spaces from a string using Stringstream Last Updated : 30 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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; } OutputThisisatest geeksforgeeks geeksquizisawesome! Ilovetocode Time complexity: O(n) where n is length of stringAuxiliary 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 OutputThisisatest geeksforgeeks geeksquizisawesome! Ilovetocode Time complexity: O(n) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Program to remove HTML tags from a given String N Nishant Shrivastava Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Removing punctuations from a given string Given a string, remove the punctuation from the string if the given character is a punctuation character, as classified by the current C locale. The default C locale classifies these characters as punctuation: ! " # $ % & ' ( ) * + , - . / : ; ? @ [ \ ] ^ _ ` { | } ~ Examples: Input : %welcome' 5 min read Remove Duplicate/Repeated words from String Given a string S, the task is to remove all duplicate/repeated words from the given string. Examples: Input: S = "Geeks for Geeks A Computer Science portal for Geeks" Output: Geeks for A Computer Science portal Explanation: here 'Geeks' and 'for' are duplicate so these words are removed from the str 4 min read Print reverse string after removing vowels Given a string s, print reverse of string and remove the characters from the reversed string where there are vowels in the original string. Examples: Input : geeksforgeeksOutput : segrfsegExplanation :Reversed string is skeegrofskeeg, removing characters from indexes 1, 2, 6, 9 & 10 (0 based ind 13 min read Program to remove HTML tags from a given String Given a string str that contains some HTML tags, the task is to remove all the tags present in the given string str. Examples: Input: str = "<div><b>Geeks for Geeks</b></div>" Output: Geeks for GeeksInput: str = "<a href="https://www.geeksforgeeks.org/">GFG</a>" O 3 min read Remove all occurrences of a character from a string using STL Given a string S and a character C, the task is to remove all the occurrences of the character C from the given string. Examples: Input:vS = "GFG IS FUN", C = 'F' Output:GG IS UN Explanation: Removing all occurrences of the character 'F' modifies S to "GG IS UN". Therefore, the required output is GG 2 min read Remove consecutive vowels from string Given a string s of lowercase letters, we need to remove consecutive vowels from the string Note : Sentence should not contain two consecutive vowels ( a, e, i, o, u). Examples : Input: geeks for geeksOutput: geks for geksInput : your article is in queue Output : yor article is in quApproach: Iterat 15+ min read Like