Given a comma-separated string, the task is to parse this string and separate the words in C++.
Input: "1, 2, 3, 4, 5" Output: 1 2 3 4 5 Input: "Geeks, for, Geeks" Output: Geeks for Geeks
- Get the string in stream – stringstream
- Create a string vector to store the parsed words
- Now 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() method
- This will give the word in the substring
- Now store this word in the vector
- This word is now removed from the stream and stored in the vector
Below is the implementation of the above approach:
1 2 3 4 5 6
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- How to input a comma separated string in C++?
- Program to parse the Molecules and get the Atoms count
- Comma in C and C++
- Comma operator should be used carefully
- Result of comma operator as l-value in C and C++
- How can we use Comma operator in place of curly braces?
- Program to Encrypt a String using ! and @
- Program for Reversed String Pattern
- C++ Program to compare two string using pointers
- Program for length of a string using recursion
- Program to print all substrings of a given string
- Program to remove vowels from a String
- C++ Program to Convert String to Integer
- Program to extract words from a given String
- Program to toggle all characters in a string
- Program to reverse words in a given string in C++
- C Program to Check if a Given String is Palindrome
- C++ Program to remove spaces from a string
- Program to duplicate Vowels in String
- Recursive program to replace all occurrences of pi with 3.14 in a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.