How to remove space from string in C++?
In C++, a string is a sequence of characters. Spaces or white spaces are also characters that can be present in a string. There are times when you may need to remove these spaces from a string, especially when processing user input or comparing strings. In this blog post, we will explore different ways of removing spaces from a string in C++.
Method 1: Using the erase-remove idiom
The erase-remove idiom is a popular way of removing elements from a container in C++. It works by calling the erase function on a container after calling the remove algorithm to move the elements to be removed to the end of the container. The remove algorithm returns an iterator to the new end of the container, which is then passed to the erase function to erase the unwanted elements. We can use this idiom to remove spaces from a string as shown below:
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int main() {
string str = " This is a string with spaces. ";
str.erase(remove(str.begin(), str.end(), ' '), str.end());
cout<< str <<endl;
return 0;
}
Output:
Thisisastringwithspaces.
In this code, we create a string with spaces and call the erase-remove idiom on it to remove the spaces. The remove algorithm takes three arguments: the beginning and end iterators of the container, and the value to be removed. We pass the iterators of the string and the space character ' ' as the value to be removed. The erase function is called on the string object with the returned iterator from remove as its argument. This removes the spaces from the string.
Method 2: Using a loop to remove spaces
We can also remove spaces from a string using a loop. In this method, we iterate over the string and remove any space characters encountered. It can be done using the erase function as shown below:
#include <string>
#include <iostream>
using namespace std;
int main() {
string str = " This is a string with spaces. ";
for (int i = 0; i<str.length(); i++) {
if (str[i] == ' ') {
str.erase(i, 1);
i--;
}
}
cout<< str <<endl;
return 0;
}
Output:
Thisisastringwithspaces.
In this code, we create a string with spaces and iterate over it using a loop. For each character encountered, we check if it is a space character. If it is, we call the erase function on the string object with the index of the space character and a count of 1 to remove the space character. We also decrement the loop index i to account for the removed character.
Method 3: Using stringstream
The stringstream class in C++ is used to convert strings to stream and from other data types. It is a useful tool for manipulating strings, and we can use it to remove spaces from a string as shown below:
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string str = " This is a string with spaces. ";
stringstream ss(str);
string result;
while (ss >> str) {
result += str;
}
cout<< result <<endl;
return 0;
}
Output:
Thisisastringwithspaces.
In this code, we create a string with spaces and pass it to a stringstream object. After that, we use a loop to extract each word from the stringstream and append it to a result string. The stringstream automatically skips any leading spaces, so the result string does not contain any spaces
Method 4: Using the replace function
We can also remove spaces from a string using the replace function in C++. The replace function replaces a portion of the string with another string or character. We can use it to replace all spaces in a string with an empty string as shown below:
#include <string>
#include <iostream>
using namespace std;
int main() {
string str = " This is a string with spaces. ";
size_t pos = str.find(' ');
while (pos != string::npos) {
str.replace(pos, 1, "");
pos = str.find(' ', pos);
}
cout<< str <<endl;
return 0;
}
Output:
Thisisastringwithspaces.
Conclusion
In this blog post, we have explored different methods of removing spaces from a string in C++. We have used the erase-remove idiom, loops, stringstream, and the replace function to achieve this. These methods can be used in different scenarios depending on the requirements of the application. By understanding how to remove spaces from a string, you can manipulate and process strings more effectively in your C++ applications.