Split a sentence into words in C++

16 Jun, 2022
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. 
CPP
// 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 codeint main(){    string str = "Geeks for Geeks";    removeDupWord(str);    return 0;}
Geeks
for
Geeks
Time complexity : O(n) Auxiliary Space : O(n)
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Method 2 (Using strtok())  
CPP
// 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 codeint main(){    char str[] = "Geeks for Geeks";    removeDupWord(str);    return 0;}
Geeks
for
Geeks
Time complexity : O(n) Auxiliary Space : O(n)
Method 3 (Using stringstream)
CPP
// 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);     string word; // for storing each word     // Traverse through all words    // while loop till we get    // strings to store in string word    while (ss >> word)    {        // print the read word        cout << word << "\n";    }} // Driver codeint main(){    string str = "Geeks for Geeks";    removeDupWord(str);    return 0;}
Geeks
for
Geeks
Time complexity : O(n) Auxiliary Space : O(n)
Split a sentence into words in C++ | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersSplit a sentence into words in C++ | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:52•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=BUy-SE6ZWGM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
abhiswaspatil1710
mahendrabagul569
cpp-string
cpp-stringstream
C++ Programs
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
delete keyword in C++
C Program to Swap two Numbers
C++ Program for QuickSort
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++