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++
// C++ program to extract words from // a strung 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 code int main() { string s = "sky is blue"; printWords(s); return 0; } |
Java
// 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 funtion. 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
# Python3 program to extract words from # a string def printWords(strr): word = strr.split(" ") print(*word, sep="\n") # Driver code s = "sky is blue"printWords(s) # This code is contributed by shubhamsingh10 |
C#
// 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 funtion. 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 |
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 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Extract and print words separately from a given Camel Case string
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- Program to reverse words in a given string in C++
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Extract all integers from the given string in Java
- Java program to print Even length words in a String
- Extract all integers from string in C++
- Python Regex to extract maximum numeric value from a string
- Program to print the given digit in words
- Count words in a given string
- Reverse words in a given String in Java
- Check if a two character string can be made using given words
- Shortest substring of a string containing all given words
- Reverse words in a given String in Python
- Check if given words are present in a string
- Reverse words in a given string | Set 2
- Reverse words in a given string
- Java program to swap first and last characters of words in a sentence

