Extract all integers from string in C++
Given a string, extract all integers words from it.
Examples :
Input : str = "geeksforgeeks 12 23 practice" Output : 12 13 Input : str = "1: Prakhar Agrawal, 2: Manish Kumar Rai, 3: Rishabh Gupta" Output : 1 2 3 Input : str = "Ankit sleeps at 4 am." Output : 4
The idea is to use stringstream:, objects of this class use a string buffer that contains a sequence of characters.
Algorithm
1. Enter the whole string into stringstream. 2. Extract the all words from string using loop. 2. Check whether a word is integer or not.
/* Extract all integers from string */#include <iostream> #include <sstream> using namespace std; void extractIntegerWords(string str) { stringstream ss; /* Storing the whole string into string stream */ ss << str; /* Running loop till the end of the stream */ string temp; int found; while (!ss.eof()) { /* extracting word by word from stream */ ss >> temp; /* Checking the given word is integer or not */ if (stringstream(temp) >> found) cout << found << " "; /* To save from space at the end of string */ temp = ""; } } // Driver code int main() { string str = "1: 2 3 4 prakhar"; extractIntegerWords(str); return 0; } |
chevron_right
filter_none
Output:
1 2 3 4
Related Articles :
- Converting string to number and vice-versa in C++
- Program to extract words from a given String
- Removing spaces from a string using Stringstream
This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Extract all integers from the given string in Java
- Program to extract words from a given String
- Python Regex to extract maximum numeric value from a string
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Number of substrings divisible by 6 in a string of integers
- How to sum two integers without using arithmetic operators in C/C++?
- Find sum of integers in a file which contains any characters
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find the missing element in an array of integers represented in binary format
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- String slicing in Python to check if a string can become empty by recursive deletion
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimal moves to form a string by adding characters or appending string itself



