Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 15 Jun, 2022

Given a String comprising of many words separated by space, the task is to iterate over these words of the string in C++.
Example:

Input: str = “GeeksforGeeks is a computer science portal for Geeks”
Output: GeeksforGeeks
             is
             a
             computer
             science
             portal
             for
             Geeks

Input: str = “Geeks for Geeks”
Output: Geeks
             for
             Geeks

Approach: istringstream class is best suitable for this purpose. When a string is given split by whitespace, this class can be used to easily fetch and use each word of the String.
Syntax:

string str = {"Geeks for Geeks"};
istringstream iss(str);

Below is the implementation of the above approach: 

CPP




// C++ program to Iterate through
// a String word by word
 
#include <iostream>
#include <sstream>
#include <string>
 
using namespace std;
 
// Driver code
int main()
{
 
    // Get the String
    string str = "GeeksforGeeks is a computer "
                 "science portal for Geeks";
 
    // Initialise the istringstream
    // with the given string
    istringstream iss(str);
 
    // Iterate the istringstream
    // using do-while loop
    do {
        string subs;
 
        // Get the word from the istringstream
        iss >> subs;
 
        // Print the word fetched
        // from the istringstream
        cout << subs << endl;
 
    } while (iss);
 
    return 0;
}

Output

GeeksforGeeks
is
a
computer
science
portal
for
Geeks

Another method: This can also be done iteratively

  1. Calculate the length of the given string say n
  2. Iterate on given string from i = 0 to i < n
  3. Check if current character str[i] == ” ” or i == n – 1
    1. Print the string formed by word and empty the word string
  4. Otherwise, keep appending characters in the word string

Below is the implementation of the 

C++




// C++ program to Iterate a string Word by Word
#include <bits/stdc++.h>
using namespace std;
 
// Function to split words from the given string.
void splitWord(string str)
{
    // Find length of given variable
    int n = str.length();
 
    // Create an empty string
    string word = "";
 
    // Iterate over the string character by character using
    // For loop
    for (int i = 0; i < n; i++) {
 
        // Check if the current iteration is equal to ' ' or
        // it's the last character
        if (str[i] == ' ' or i == (n - 1)) {
 
            // Print word
            cout << word + str[i] << endl;
            word = "";
        }
 
        // Add current character in word string
        else {
            word += str[i];
        }
    }
}
 
int main()
{
    // Given string
    string str = "GeeksforGeeks is a computer "
                 "science portal for Geeks";
 
    splitWord(str);
 
    return 0;
}
// This code is contributed by Shivesh Kumar Dwivedi

Output

GeeksforGeeks 
is 
a 
computer 
science 
portal 
for 
Geeks

Time Complexity: O(n), Where n is the size of the given string
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!