![]() |
VOOZH | about |
Given a String comprising of many words separated by space, the task is to iterate over these words of the string in C++.
Example:
Input: str = "GeeksforGeeks is a computer science portal for Geeks"
Output: GeeksforGeeks
is
a
computer
science
portal
for
GeeksInput: str = "Geeks for Geeks"
Output: Geeks
for
Geeks
Approach: istringstream class is best suitable for this purpose. When a string is given split by whitespace, this class can be used to easily fetch and use each word of the String.
Syntax:
string str = {"Geeks for Geeks"};
istringstream iss(str);
Below is the implementation of the above approach:
GeeksforGeeks is a computer science portal for Geeks
Another method: This can also be done iteratively
Below is the implementation of the
GeeksforGeeks is a computer science portal for Geeks
Time Complexity: O(n), Where n is the size of the given string
Auxiliary Space: O(1)