
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to take out integer from comma separated string
Suppose we have a string where we have few integers which are separated by comma. We shall have to separate them out and display each integer in different line. To do this we shall use stringstream (under sstream library) in C++. This is one string based stream class present in C++. We can use extraction operator (<<) to extract something, insertion operator (>>) to insert something and str() functions to set contents of underlying string device object.
So, if the input is like s = "56,9,85,256,47", then the output will be
56 9 85 256 47
To solve this, we will follow these steps −
Define an array take_int(string str)
create one stringstream object called ss
Define an array result
while new integer item taken from ss into temp is not null, do:
insert tmp at the end of result
skip comma character by taking out single character
return result
From the main method do the following:
Define an array integers = take_int(s)
for initialize i := 0, when i < size of integers, update (increase i by 1), do:
display integers[i]
Example
Let us see the following implementation to get better understanding −
#include <iostream> #include <sstream> #include <vector> using namespace std; vector<int> take_int(string str) { stringstream ss(str); vector<int> result; char ch; int tmp; while(ss >> tmp) { result.push_back(tmp); ss >> ch; } return result; } int main(){ string s = "56,9,85,256,47"; vector<int> integers = take_int(s); for(int i = 0; i < integers.size(); i++) cout << integers[i] << "\n"; }
Input
56,9,85,256,47
Output
56 9 85 256 47
- Related Articles
- Python program to convert list of string to comma separated string
- How to create a comma separated string from a list of string in C#?
- Java Program to Convert a List of String to Comma Separated String
- Swift Program to Convert an Set of String to Comma Separated String
- MySQL query to get a single value from position of comma-separated string?
- Find integer in text data (comma separated values) with MySQL?
- Convert String into comma separated List in Java
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- How to sum a comma separated string (string with numbers) in MySQL?
- Searching from a comma separated MySQL column?
- How would you make a comma-separated string from a list in Python?
- Count values from comma-separated field in MySQL?
- Remove specific word in a comma separated string with MySQL
- Java Program to convert from integer to String
