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.
- Java program to print Even length words in a String
- Check if the given string of words can be formed from words present in the dictionary
- Extract all integers from string in C++
- Extract all integers from the given string in Java
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Python Regex to extract maximum numeric value from a string
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Count words in a given string
- Reverse words in a given string
- Reverse words in a given String in Java
- Reverse String according to the number of words
- Shortest substring of a string containing all given words
- Reverse middle words of a string
- Reverse words in a given String in Python
- Minimum Distance Between Words of a String
Improved By : Animesh_Gupta