Program to extract words from a given String

11 Jun, 2021
The task is to extract words from a given string. There may be one or more space between words.
Examples: 
Input : geeks for geeks
Output : geeks
         for
         geeks

Input : I love   coding.
Output: I 
         love
         coding
We have discussed a solution in the below post. How to split a string in C/C++, Python and Java?
In this post, a new solution using stringstream is discussed. 
1. Make a string stream.
2. extract words from it till there are still words in the stream.
3. Print each word on new line.
This solution works even if we have multiple spaces between words. 
C++
Java
Python3
C#
Javascript
// C++ program to extract words from// a string using stringstream#include<bits/stdc++.h>using namespace std; void printWords(string str){    // word variable to store word    string word;     // making a string stream    stringstream iss(str);     // Read and print each word.    while (iss >> word)        cout << word << endl;} // Driver codeint main(){    string s = "sky is blue";    printWords(s);    return 0;}
// A Java program for splitting a string// using split()import java.io.*;class GFG{    // Method splits String into    // all possible tokens    public static void printWords(String s)    {            // Using split function.        for (String val: s.split(" "))                     // printing the final value.            System.out.println(val);    }         // Driver Code    public static void main(String args[])    {        // Sample string to check the code        String Str = "sky is blue";                 // function calling        printWords(Str);    }} // This code is contributed// by Animesh_Gupta
# Python3 program to extract words from# a stringdef printWords(strr):    word = strr.split(" ")    print(*word, sep="\n") # Driver codes = "sky is blue"printWords(s) # This code is contributed by shubhamsingh10
// A C# program for splitting a string// using split()using System;class GFG{    // Method splits String into    // all possible tokens    public static void printWords(String s)    {            // Using split function.        foreach(String val in s.Split(" "))                     // printing the final value.            Console.WriteLine(val);    }         // Driver Code    static public void Main ()    {        // Sample string to check the code        String Str = "sky is blue";                 // function calling        printWords(Str);    }} // This code is contributed// by shivanisingh
<script> // A Javascript program for splitting// a string using split() // Method splits String into// all possible tokensfunction printWords(s){       s = s.split(" ");    for(let val = 0; val < s.length; val++)    {        document.write(s[val] + "</br>");    }} // Driver code // Sample string to check the codelet Str = "sky is blue"; // function callingprintWords(Str); // This code is contributed by divyeshrabadiya07 </script>
Output: 
sky
is
blue
This article is contributed by Tanya Anand. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Animesh_Gupta
SHUBHAMSINGH10
shivanisinghss2110
akshaysingh98088
divyeshrabadiya07
cpp-string
cpp-stringstream
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
What is Data Structure: Types, Classifications and Applications
Print all the duplicates in the input string
Print all subsequences of a string
A Program to check if strings are rotations of each other or not
String class in Java | Set 1
Find if a string is interleaved of two other strings | DP-33
Remove first and last character of a string in Java
Find the smallest window in a string containing all characters of another string
Program to count occurrence of a given character in a string