Extract all integers from string in C++
Given a string, extract all integers words from it. Examples :
Input : str = "geeksforgeeks 12 13 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.
- Enter the whole string into stringstream.
- Extract the all words from string using loop.
- Check whether a word is integer or not.
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 firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.