C++ Check If a String Contains Only Numbers
This article will guide you on how to check if a given string in C++ contains only numbers. We'll explore various methods, each with its advantages and use cases.
1. Using isdigit()
function
The isdigit()
function is a standard C++ library function that checks if a given character is a digit (0-9). You can iterate through each character of the string and check if it's a digit.
#include
#include
#include
bool isStringOnlyNumbers(const std::string& str) {
for (char c : str) {
if (!std::isdigit(c)) {
return false;
}
}
return true;
}
int main() {
std::string str1 = "12345";
std::string str2 = "abc123";
if (isStringOnlyNumbers(str1)) {
std::cout << "String 1 contains only numbers" << std::endl;
} else {
std::cout << "String 1 contains non-numeric characters" << std::endl;
}
if (isStringOnlyNumbers(str2)) {
std::cout << "String 2 contains only numbers" << std::endl;
} else {
std::cout << "String 2 contains non-numeric characters" << std::endl;
}
return 0;
}
2. Using std::all_of()
with isdigit()
The std::all_of()
algorithm from <algorithm>
can be used to check if all elements in a range satisfy a certain condition. You can use it in conjunction with isdigit()
to efficiently check if all characters in the string are digits.
#include
#include
#include
#include
bool isStringOnlyNumbers(const std::string& str) {
return std::all_of(str.begin(), str.end(), ::isdigit);
}
int main() {
std::string str1 = "12345";
std::string str2 = "abc123";
if (isStringOnlyNumbers(str1)) {
std::cout << "String 1 contains only numbers" << std::endl;
} else {
std::cout << "String 1 contains non-numeric characters" << std::endl;
}
if (isStringOnlyNumbers(str2)) {
std::cout << "String 2 contains only numbers" << std::endl;
} else {
std::cout << "String 2 contains non-numeric characters" << std::endl;
}
return 0;
}
3. Using std::regex
The std::regex
class provides regular expression matching capabilities. You can create a regular expression that matches strings containing only digits and use std::regex_match()
to check if the input string matches the pattern.
#include
#include
#include
bool isStringOnlyNumbers(const std::string& str) {
std::regex pattern("[0-9]+");
return std::regex_match(str, pattern);
}
int main() {
std::string str1 = "12345";
std::string str2 = "abc123";
if (isStringOnlyNumbers(str1)) {
std::cout << "String 1 contains only numbers" << std::endl;
} else {
std::cout << "String 1 contains non-numeric characters" << std::endl;
}
if (isStringOnlyNumbers(str2)) {
std::cout << "String 2 contains only numbers" << std::endl;
} else {
std::cout << "String 2 contains non-numeric characters" << std::endl;
}
return 0;
}
4. Using std::stoi()
with std::invalid_argument
The std::stoi()
function attempts to convert a string to an integer. If the string contains non-numeric characters, it throws an std::invalid_argument
exception. You can catch this exception to determine if the string contains only numbers.
#include
#include
#include
bool isStringOnlyNumbers(const std::string& str) {
try {
std::stoi(str);
return true;
} catch (const std::invalid_argument& e) {
return false;
}
}
int main() {
std::string str1 = "12345";
std::string str2 = "abc123";
if (isStringOnlyNumbers(str1)) {
std::cout << "String 1 contains only numbers" << std::endl;
} else {
std::cout << "String 1 contains non-numeric characters" << std::endl;
}
if (isStringOnlyNumbers(str2)) {
std::cout << "String 2 contains only numbers" << std::endl;
} else {
std::cout << "String 2 contains non-numeric characters" << std::endl;
}
return 0;
}
Conclusion
Choosing the best method depends on your specific needs and coding style. The isdigit()
and std::all_of()
methods offer a simple and straightforward approach. std::regex
provides flexibility for more complex pattern matching. The std::stoi()
method is less explicit but may be suitable for scenarios where you also need to convert the string to an integer.