Program to extract words from a given String
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
Time complexity: O(n) where n is size of input string, since using a while loop
Auxiliary Space: O(1)
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 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.
Please Login to comment...