Extract all integers from string in C++ Last Updated : 14 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Algorithm: Enter the whole string into stringstream. Extract the all words from string using loop. Check whether a word is integer or not. Implementation: CPP /* 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; } Output1 2 3 4 Time Complexity: O(N), where, N is the length of the string. Auxiliary Space: O(1), We are not using any extra space. Related Articles : Converting string to number and vice-versa in C++Program to extract words from a given StringRemoving spaces from a string using Stringstream Comment More infoAdvertise with us Next Article Convert String to size_t in C++ P Prakhar Agrawal Improve Article Tags : C++ cpp-string Practice Tags : CPP Similar Reads Convert String to int in C++ In C++, both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. There are 6 significant methods to convert strings to numbers in C++ as follows:1. String to int Conversion Using st 6 min read Remove Leading Zeros From String in C++ Given a string of digits, remove leading zeros from it. Examples: Input : 00000123569 Output : 123569 Input : 000012356090 Output : 12356090 In this article, we will use two string functions i.e, string erase and stoi() to remove leading zeros from the string. 1. Using String Erase FunctionCount tra 2 min read getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It extracts the characters from the input stream and appends it to the given string object until the delimiting character is encountered.Example:C++#include <bits/stdc++.h> using name 3 min read Convert String to size_t in C++ To convert String to size_t in C++ we will use stringstream, It associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). We must include the stream header file in order to use stringstream. When parsing input, the stringstream class comes in qu 1 min read Converting Number to String in C++ In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at som 4 min read String Functions in C++ A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar 8 min read Like