How to iterate over the words of a string in C++?

How to iterate over the words of a string in C++?

We can iterate over the words of a string with the help of std::stringstream, iterators & find() methods as these are the most common methods used to break down the string into words one by one.

In C++, processing text data efficiently is a common operation, and one frequently encountered task is iterating over the words in a string. There are a few methods to achieve iteration over the words. In this article, we will be discussing how traditional and modern techniques can efficiently iterate over the individual words in a given string in C++.

Table of Contents:

Methods to Iterate through a String word by word in C++

Given below are the few methods that help to extract the words from a string, including using stringstream, iterators, regex, and then view split.

Method 1: Using std::stringstream (Traditional Approach) in C++

The std::stringstream is the simplest and most common method, and it treats a string as a stream and uses the extraction operator (>>) to extract words from the string in C++. You can use std::stringstream when you are dealing with simple words that are separated by whitespace. It is an effective approach for fast primary text processing.

Example:

Cpp

Output:

In this program, std::stringstream is created with a string. The while loop extracts each word one at a time by treating whitespaces as the boundary between each word, where every word gets printed on a new line.

Method 2: Using Iterators and find() in C++

Using the iterators and find() in C++ is a manual technique to find whitespace in the string and iterate over the words. You can use this method when some specific delimiters are to be implemented.

Example:

Cpp

Output:

In this code, str::string::find() checks for whitespace, separates substrings into different words, and iterates over them.

Method 3: Using the Regular Expressions (regex) in C++

In C++, regular expressions are used for pattern matching. You can use this method when you are dealing with strings having alphabets followed by numbers. It provides maximum flexibility for specifying the splitting criteria.

Example

Cpp

Output:

In this program, std::sregex_iterator is used to iterate through the strings. It checks if the entire string matches the pattern or not and then gives the result based on the following sequences.

Method 4: Using std::views::split in C++20

In version C++20, a modern approach std::view::split is introduced in the ranges library. It iterates over the words in a very efficient and convenient manner. It allows you to divide or split the string in the words using the view::split() function based on the given delimiters or whitespace. 

Example:

Cpp

Output:

In this code, std::view::split is used to split or iterate over the words of a string. We need to include <ranges> in the header to include the library and the split() function to get the required results. Also, ‘,’ is taken as a delimiter.

Conclusion

Every method used for iterating over the words of a string in C++ has its advantages. The std::stringstream method is simple and easier. Using iterators and find() allows for more control. The std::regex method provides flexibility with pattern matching. For a modern and efficient solution, C++20’s std::views::split is the best choice.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner