Program to Parse a comma separated string in C++ Last Updated : 22 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a comma-separated string, the task is to parse this string and separate the words in C++. Examples: Input: "1,2,3,4,5" Output: 1 2 3 4 5 Input: "Geeks,for,Geeks" Output: Geeks for Geeks Approach: Get the string in stream - stringstreamCreate a string vector to store the parsed wordsNow till there is a string in stringstream, checked by good() method,Get the substring if the string from starting point to the first appearance of ', ' using getline() methodThis will give the word in the substringNow store this word in the vectorThis word is now removed from the stream and stored in the vector Below is the implementation of the above approach: CPP // C++ program to parse a comma-separated string #include <bits/stdc++.h> using namespace std; int main() { string str = "1,2,3,4,5,6"; vector<string> v; stringstream ss(str); while (ss.good()) { string substr; getline(ss, substr, ','); v.push_back(substr); } for (size_t i = 0; i < v.size(); i++) cout << v[i] << endl; } Output: 1 2 3 4 5 6 Time Complexity: O(N), as we are using a single loop to iterate over the entire string.Auxiliary Space: O(N), as we are storing all the tokens in a vector. Comment More infoAdvertise with us Next Article Program to Parse a comma separated string in C++ C code_r Follow Improve Article Tags : Strings C++ Programs C++ DSA Practice Tags : CPPStrings Similar Reads 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 C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf(" 2 min read C++ Program to Split a String Into a Number of Sub-Strings Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C++Note: The main disadvantage of strtok() 3 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 Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read Like