Split a sentence into words in C++

Give a sentence, print different words present in it. Words are separated by space.

Examples:

Input : str = "Geeks for Geeks"
Output : Geeks
         for
         Geeks  
Explanation : All space separated words 
are printed line by line.

Input : str = "a computer science portal"
Output : a
         computer
         science
         portal

Method 1 (Writing our own logic)
We traverse through all characters. If current character is space, we have reached end of a word, we print current word and reset it to empty. Else we append current character to word.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print words in a sentence
#include <bits/stdc++.h>
using namespace std;
  
void removeDupWord(string str)
{
   string word = "";
   for (auto x : str)
   {
       if (x == ' ')
       {
           cout << word << endl;
           word = "";
       }
       else
       {
           word = word + x;
       }
   
   cout << word << endl;
}
  
// Driver function
int main()
{
    string str = "Geeks for Geeks";
    removeDupWord(str);
    return 0;
}

chevron_right


Output:

Geeks
for
Geeks

Method 2 (Using strtok())



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to words in a sentence.
#include <bits/stdc++.h>
using namespace std;
  
void removeDupWord(char str[])
{
    // Returns first token 
    char *token = strtok(str, " ");
   
    // Keep printing tokens while one of the
    // delimiters present in str[].
    while (token != NULL)
    {
        printf("%s\n", token);
        token = strtok(NULL, " ");
    }
}
  
// Driver function
int main()
{
    char str[] = "Geeks for Geeks";
    removeDupWord(str);
    return 0;
}

chevron_right


Output:

Geeks
for
Geeks

Method 3 (Using stringstream)

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print words in a sentence
#include <bits/stdc++.h>
using namespace std;
  
void removeDupWord(string str)
{
    // Used to split string around spaces.
    istringstream ss(str);
  
    // Traverse through all words
    do {
        // Read a word
        string word;
        ss >> word;
  
        // Print the read word
        cout << word << endl;
  
        // While there is more to read
    } while (ss);
}
  
// Driver function
int main()
{
    string str = "Geeks for Geeks";
    removeDupWord(str);
    return 0;
}

chevron_right


Output:

Geeks
for
Geeks

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.