Iterate through a String word by word in C++

In this article, we will learn about how to iterate through a string word by word in C++.

So, continue reading through the article……

Simple approach

There will be a number of words separated by spaces in a string.
The aim is to iterate through a string word by word.
To solve our problem we will use istringstream class.
From the given string is gets the string and uses every word one by one.

Example:

Input:

 str= "Anyone can learn any language very easily";

Output:

Anyone                                                                                                                          
can                                                                                                                             
learn                                                                                                                           
any                                                                                                                             
language                                                                                                                       
very                                                                                                                            
easily

C++ program to iterate through a string word by word

Now, its time to write the code to perform our task. So below is our C++ code:

#include <iostream> 
#include <sstream> 
#include <string> 
using namespace std; 
  
int main() 
{ 
    string str = "Anyone can learn any language very easily"; 
    istringstream iss(str); 
    do 
    { 
        string str2; 
        iss >> str2; 
        cout << str2 << endl; 
        
    } while (iss); 
  
    return 0; 
}

The output of the following code:

Anyone                                                                                                                          
can                                                                                                                             
learn                                                                                                                           
any                                                                                                                             
language                                                                                                                       
very                                                                                                                            
easily

Explanation of the following code:

The explanation will help you to understand the code better.

In this code, first of all, we will declare the string. Then we will declare and initialize the istringstream with the original string. With the help of the do-while loop, we will iterate through the string. This way, each and every word will get printed on the output screen.

I hope that this will help you to solve your problem.

You may also read:

String push_back() function in C++

String replace() function in C++

Leave a Reply