How to input a comma separated string in C++? Last Updated : 06 Feb, 2023 Comments Improve Suggest changes 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 input a comma separated string in C++? C code_r Follow Improve Article Tags : Strings C++ Programs C++ DSA Practice Tags : CPPStrings Similar Reads Program to Parse a comma separated string in C++ 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 th 1 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 Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to 2 min read How to Split a String into an Array in C++? 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 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Use stringstream for Input With Spaces in C++? In C++, the std::stringstream class is a stream class to operate on strings and is very useful when we want to operate on a string as if it were a stream (like cin or cout). In this article, we will learn how to use string streams for input with spaces in C++.Example:Input: string = âHello, World!âO 2 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read How to Read a Line of Input Text in C++? In C++, we often need to take input from the user by reading an input text line by line but the cin method only takes input till whilespace. In this article, we will look at how to read a full line of input in C++. For Example, Input: This is a text.Output: Entered Text: This is a text.Taking a Line 2 min read How to Take Long String Input With Spaces in C++? In C++, we often take long strings with a lot of characters as inputs with spaces but if we use cin then our input gets terminated as soon as whitespace is encountered. In this article, we will discuss how to take long strings with spaces as input in C++. Take Long String Input With Spaces in C++To 2 min read Like