Remove Spaces from std::string in C++



In this article, we will learn all the different approaches to remove the whitespaces from a standard string in C/C++. First of all, let's understand our problem statement.

The input of this problem is a non-empty string containing multiple characters and whitespaces between those characters. Our task is to print the string by ignoring all the whitespaces into the output console. For example:

// Input String
"This is a string"

// Output String
"Thisisastring"

Remove Whitespaces from a String in C++

Here is the list of approaches to remove all the whitespaces from a string using c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.

Using remove() and erase() Functions to Remove Whitespaces

This method uses the remove() and erase() functions to remove spaces in place using the erase-remove idiom. In this method the remove() function shifts non-space characters to fronts and the erase() function trancates remining part. This avoid creating a new string for running program as the code runs in place itself. Let's see how to implement this method.

Example

In the code below, we removed all the whitespaces in the string "C++ is Best" using standard remove() and erase() functions.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string str = "C++ is Best";
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    cout << str;  // Output: C++isfun
}

The output of the above code will be:

C++isBest

Remove Whitespaces By Creating New String

This approach involves iterating over each characters in the string and adding non-space characters to a new string. This is an easy to understand method, and also you can modify the string as per wish, such as remove tabs, newlines etc. However, this method require additional memory to create and store new string.

Example

In the code below, we have copied all the characters in the string "Tutorials point" expect the whitespace to the result string.

#include <iostream>
using namespace std;

int main() {
    string str = "Tutorials point";
    string result;
    for (char c : str) {
        if (c != ' ') result += c;
    }
    cout << result; 
}

The output of the above code will be:

Tutorialspoint

Using Regular Expression to Remove Whitespaces

The regular expression is used to match and edit a string pattern in a larger string. In this method, we will use regex_replace() function to match all whitespaces inside the string and replace it with empty piece of string. Let's see how to do it below.

Example

In the code below, we used the regex_replace() function to replace all whitespace in the string "C++ is fun" to an empty piece of string ("").

#include <iostream>
#include <regex>
using namespace std;

int main() {
    string str = "C++ is fun";
    string result = regex_replace(str, regex(" "), "");
    cout << result;  
}

The output of the above code will be:

C++isfun
Updated on: 2025-04-23T18:48:16+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements