Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract all integers from string in C++
In this article, we will discuss how to extract all the integers from a string using C++ program. We will explore all the possible approaches to do so. First of all, let's understand the problem statement.
We have a string that contains a mix of digits and non-digits. We need to extract all the integers from the string and store them in a vector. The integers can be positive or negative. For example,
// Input String
string str = "ab24wj-123fow"
// Output Vector
vector<int> vec = {24, -123}
Approaches to Extract Integers from String
Here is the list of approaches to extract integers from a string in C++ that we will be discussing in this article with stepwise explanation and complete example codes.
Using Regular Expressions to Extract Integers
The regular expressions are used to match a pattern in a string. In this approach, we will use the c++ regex library to extract all the integers from the string and store them in a vector. This method is only supported in C++ 11 and above. versions. Let's see an example of how to use this approach.
Example
In the code below, we defined a input string "abc123x4y56" and a regular expression "\d+" to extract all the integers from the string. We then used the sregex_iterator class to iterate through the matches and stored them in a vector. Finally, we printed the vector to see the output.
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
int main() {
string input = "23abc123x4y56";
regex numberRegex(R"(\d+)");
sregex_iterator it(input.begin(), input.end(), numberRegex);
sregex_iterator end;
vector<int> numbers;
while (it != end) {
numbers.push_back(stoi(it->str()));
++it;
}
for (int n : numbers)
cout << n << " ";
}
The output of the above code will be:
23 123 4 56
Using Stringstream to Extract Integers
The stringstream is an object of string class defined in the <sstream> header file. This approach involves using a stringstream object to remove all the non-digits from the string and then converting the remaining string to an integer. Let's see an example of how to use this approach.
Example
In the code below, we defined a input string "abc123x4y56" and a stringstream object ss. We then used the isdigit function to check if the character is a digit or not in the string. If it is a digit, we added it to the stringstream object. Otherwise, we added a blank space to the stringstream object. After that, we iterated through the stringstream object and converted the remaining string to an integer and stored it in a vector.
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string input = "abc123x4y56";
stringstream ss;
vector<int> numbers;
// Remove all non-digits from the string
for (char c : input) {
if (isdigit(c)) ss << c;
else ss << ' ';
}
// Convert the remaining string to an integer and store it
int num;
while (ss >> num)
numbers.push_back(num);
// Print the vector
for (int n : numbers)
cout << n << " ";
}
The output of the above code will be:
123 4 56
Using String Tokenizer to Extract Integers
The string tokenizer is a class defined in the <string> header file. This approach involves using a string tokenizer object to extract all the integers from the string and store them in a vector. Let's see an example of how to use this approach.
Example
In the code below, we defined a input string "abc123x4y56" and a string tokenizer object tok. We then used the getToken() function to extract all the integers from the string and stored them in a vector. Finally, we printed the vector to see the output.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string input = "Tutorials23Point3224";
vector<int> numbers;
string token;
for (char c : input) {
if (isdigit(c)) {
token += c;
} else {
if (!token.empty()) {
numbers.push_back(stoi(token));
token.clear();
}
}
}
if (!token.empty()) {
numbers.push_back(stoi(token)); // Handle trailing number
}
for (int n : numbers)
cout << n << " ";
}
The output of the above code will be:
23 2324
Using Manual Loop to Extract Integers
In this approach, we will run a loop through the string and check if the character is a digit or not. If it is a digit, we add it to a vector.
Example
In the code below, we run a loop through the string and check if the character is a digit or not. If it is a digit, we add it to a vector.
#include <iostream>
#include <vector>
using namespace std;
int main() {
string input = "This is a string with numbers 123 and 456";
vector<int> numbers;
int current = 0;
bool inNumber = false;
for (char c : input) {
if (isdigit(c)) {
current = current * 10 + (c - '0');
inNumber = true;
} else if (inNumber) {
numbers.push_back(current);
current = 0;
inNumber = false;
}
}
if (inNumber)
numbers.push_back(current); // for trailing numbers
for (int n : numbers)
cout << n << " ";
}
The output of the above code will be:
123 456