VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-extract-words-given-string/

⇱ Program to extract words from a given String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to extract words from a given String

Last Updated : 23 Jul, 2025

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. 

Output: 

sky
is
blue

Time complexity: O(n) where n is size of input string, since using a while loop

Auxiliary Space: O(1)
 

Comment
Article Tags: