How to input a comma separated string in C++? Last Updated : 06 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ is very easy. The program to do so is: C++ #include <bits/stdc++.h> using namespace std; int main() { string str; // Get the string getline(cin, str); // Print the words cout << str; } Input: 1 2 3 4 5 6Output: 1 2 3 4 5 6 Why can't we use the above code for comma-separated input string? The above code works fine for a whitespace-separated input string, but for a comma-separated input string, it won't work as expected, because the program will take complete input as a single word in the string. Input: 1, 2, 3, 4, 5, 6Output: 1, 2, 3, 4, 5, 6 How to input a comma separated string? Now inorder to input a comma separated string, following approach can be used: Get the string to be taken as input in stringstreamTake each character of the string one by one from the streamCheck if this character is a comma (', ').If yes, then ignore that character.Else, Insert this character in the vector which stores the words Below is the implementation of the above approach: CPP // C++ program to input // a comma separated string #include <bits/stdc++.h> using namespace std; int main() { // Get the string string str = "11,21,31,41,51,61"; vector<int> v; // Get the string to be taken // as input in stringstream stringstream ss(str); // Parse the string for (int i; ss >> i;) { v.push_back(i); if (ss.peek() == ',') ss.ignore(); } // Print the words for (size_t i = 0; i < v.size(); i++) cout << v[i] << endl; } Output: 11 21 31 41 51 61 Time complexity: O(N). Auxiliary Space: O(N). Comment More infoAdvertise with us Next Article How to Iterate through a String word by word in C++ C code_r Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads How to split a string in C/C++, Python and Java? 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: // Splits str[] according to given delim 7 min read How to Iterate through a String word by word in C++ 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 Geeks Input: str = "Geeks for Geeks"Output: Geek 3 min read Convert std::string to LPCWSTR in C++ C++ provides us a facility using which one can represent a sequence of characters as an object of a class. This class is std::string. Internally, std::string class represents a string as a sequence of characters (bytes) and each character can be accessed using the [] operator. The difference between 2 min read std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill 2 min read Strings in C++ and How to Create them? Strings in C++ are used to store text or sequences of characters. In C++ strings can be stored in one of the two following ways: C-style string (using characters)String class Each of the above methods is discussed below: 1. C style string: In C, strings are defined as an array of characters. The dif 2 min read How to Take Input in Array in C++? Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th 3 min read Like