Determine if a string is numeric in C++
This post will discuss how to determine if a string is numeric in C++. The solution should check if the string contains a sequence of digits.
1. Using Loop
A simple solution is to iterate over the string until a non-numeric character is encountered. If all characters are digits, the string is numeric. This solution does not work with negatives or floating-point numbers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> bool isNumeric(std::string const &str) { auto it = str.begin(); while (it != str.end() && std::isdigit(*it)) { it++; } return !str.empty() && it == str.end(); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
2. Using std::find_if
We can also use the standard algorithm std::find_if from the <algorithm> header, which accepts a predicate to find the element in the specified range. It can be used as follows to find the position of the first non-digit character. If all characters in the string are digits, the string must be numeric. This solution only works for positive integers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { auto it = std::find_if(str.begin(), str.end(), [](char const &c) { return !std::isdigit(c); }); return !str.empty() && it == str.end(); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
3. Using string::find_first_not_of
The string::find_first_not_of function searches the string for the first character that does not match any of the specified characters. It can be used as follows to determine if a string is numeric.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { return !str.empty() && str.find_first_not_of("0123456789") == std::string::npos; } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
4. Using std::all_of
Starting with C++11, we can use the std::all_of function that returns true if the specified predicate holds for all the elements in the specified range. It can be used as follows to determine if a string is numeric.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
5. Using strtol() function
Alternatively, we can use the strtol() C standard library function that parse a C-string str as an integral number in the specified base and return a zero value if conversion is not possible. It can be used to parse a std::string and determine if the string is numeric, as shown below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> bool isNumeric(std::string const &str) { char* p; strtol(str.c_str(), &p, 10); return *p == 0; } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
6. Using Boost
If you’re already using boost library, you can parse the C++ string with boost::lexical_cast<>() function. It throws an boost::bad_lexical_cast exception if conversion is not possible. This also works for negative integers and floating-point numbers. Here’s the complete code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <string> #include <boost/lexical_cast.hpp> bool isInteger(std::string const &str) { try { boost::lexical_cast<int>(str); return true; } catch(boost::bad_lexical_cast &e) { return false; } } bool isDouble(std::string const &str) { try { boost::lexical_cast<double>(str); return true; } catch(boost::bad_lexical_cast& e) { return false; } } int main() { std::string str = "100"; std::cout << std::boolalpha << isInteger(str) << std::endl; // true return 0; } |
7. Using Regex
Finally, we can use a regex to determine if a string is numeric. The following solution currently works for positive and negative integers. It can be easily extended to work with floating-point numbers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <regex> bool isNumeric(std::string const &str) { return std::regex_match(str, std::regex("[(-|+)|][0-9]+")); // "[(-|+)|][[:digit:]]+" } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
That’s all about determining if a string is numeric in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)