![]() |
VOOZH | about |
In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++.
Example:
Input: str= “Hello, I am Geek from geeksforgeeks” Delimiter= ’ ’ Output: Hello, I am Geek from geeksforgeeks
To split a string into an array of substrings in C++, we can use the std::istringstream class from the <sstream> library to create an input stream from the string. We can then split the string based on some delimiter using getline() and store them into the array of strings.
std::istringstream.std::getline to extract each substring separated by the delimiter.The below example demonstrates how we can split a given string into an array of substrings in C++.
Hello, I am Geek from Geeksforgeeks
Time complexity: O(N), here N is the length of the input string.
Auxiliary Space: O(N)