The task is to extract words from a given string. There may be one or more space between words.
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.
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 email@example.com. 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.