How to Split a String into an Array in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 geeksforgeeksSplitting a String into an Array in C++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. ApproachCreate an input string stream from the input string using std::istringstream.Iterate through the stream, using std::getline to extract each substring separated by the delimiter.Add the extracted substring to the array.Print the array of substrings.C++ Program for Splitting a String into an ArrayThe below example demonstrates how we can split a given string into an array of substrings in C++. C++ // C++ Program to illustrate how to split a string into an // array of substrings #include <iostream> #include <sstream> #include <string> using namespace std; // Function to split a string into tokens based on a // delimiter void splitString(string& input, char delimiter, string arr[], int& index) { // Creating an input string stream from the input string istringstream stream(input); // Temporary string to store each token string token; // Read tokens from the string stream separated by the // delimiter while (getline(stream, token, delimiter)) { // Add the token to the array arr[index++] = token; } } int main() { // Input string string input = "Hello, I am Geek from Geeksforgeeks"; // Delimiter char delimiter = ' '; // Array to store the substrings string arrayOfSubStr[100]; // Index to keep track of the number of substrings int index = 0; // Calling the function to split the input string into // an array of substrings splitString(input, delimiter, arrayOfSubStr, index); // Print the array of substrings for (int i = 0; i < index; i++) { cout << arrayOfSubStr[i] << endl; } return 0; } OutputHello, I am Geek from Geeksforgeeks Time complexity: O(N), here N is the length of the input string.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Split a String into an Array in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-array substring cpp-strings CPP Examples +3 More Practice Tags : CPPcpp-strings Similar Reads How to Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Split a String by a Delimiter in C++? Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.ExamplesInput: str = "geeks,for,geeks", delimiter = (,)Output: gee 4 min read How to Declare Pointer to an Array of Strings in C++? In C++, an array of a string is used to store multiple strings in contiguous memory locations and is commonly used when working with collections of text data. In this article, we will learn how to declare a pointer to an array of strings in C++. Pointer to Array of String in C++If we are working wit 2 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 2 min read How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read Array of Pointers to Strings in C++ In C++, an array is a homogeneous collection of data that is stored in a contiguous memory location. We can store almost all types of data as array elements. In this article, we will learn how to store the array of pointers to strings in C++. Array of Pointers to Strings in C++A pointer to a string 6 min read How to input a comma separated string in C++? Given an input string which is comma-separated instead of space, the task is to parse this input string in C++.First, let us understand what difference does it create if the input string is comma-separated. Taking input a whitespace-separated stringTaking input a whitespace-separated string in C++ i 2 min read How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like