Remove Consecutive Whitespace

Learn to remove consecutive whitespace using different functions of C++20.

We'll cover the following

When receiving input from users, it's common to end up with excessive consecutive whitespace characters in our strings. This recipe presents a function for removing consecutive spaces, even when it includes tabs or other whitespace characters.

How to do it

This function leverages the std::unique() algorithm to remove consecutive whitespace characters from a string.

  • In the bw namespace, we start with a function to detect whitespace:

Get hands-on with 1400+ tech skills courses.