How to Check if String Is a Number C++
- HowTo
- C++ Howtos
- Determine if a String Is a Number in C++
Determine if a String Is a Number in C++
Created: September-24, 2020 | Updated: March-30, 2021
- Use
std::isdigit
Method to Determine if a String Is a Number - Use
std::isdigit
Withstd::ranges::all_of
to Determine if a String Is a Number - Use
find_first_not_of
Method to Determine if a String Is a Number
This article explains how to find out if a given C++ string is a number. Before we dig in, it should be noted that the following methods are only compatible with single-byte character strings and decimal integral numbers.
Use std::isdigit
Method to Determine if a String Is a Number
The first version is probably the most obvious way to implement the solution. Namely, pass a string as a parameter to a function isNumber
, which iterates over every single char
in the string
and checks with isdigit
method. When it finds the first non-number the function returns false, if none is found returns true.
#include <iostream> using std::cout; using std::cin; using std::endl using std::string; bool isNumber(const string& str) { for (char const &c : str) { if (std::isdigit(c) == 0) return false; } return true; } int main(){ string str1 = "231524randstr23"; string str2 = "23152423"; string str3 = "a3152423"; isNumber(str1) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str2) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str3) ? cout << "Number\n" : cout << "Not number\n"; return EXIT_SUCCESS; }
Output:
Not number Number Not number
Notice that we output verdict on each string
via ? :
ternary conditional operator, which is a concise variant of if-else
.
Use std::isdigit
With std::ranges::all_of
to Determine if a String Is a Number
The previous method was quite basic for the mighty C++, so let's implement a more eloquent solution using C++20 method std::ranges::all_of
and some lambda expressions. In our case ranges::all_of
checks if the specified lambda returns true for every element of the given range s.begin(), s.end()
and returns true if condition is satisfied.
#include <iostream> #include <algorithm> using std::cout; using std::cin; using std::endl using std::string; bool isNumber(const string& s) { return std::ranges::all_of(s.begin(), s.end(), [](char c){ return isdigit(c) != 0; }); } int main(){ string str1 = "231524randstr23"; string str2 = "23152423"; string str3 = "a3152423"; isNumber(str1) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str2) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str3) ? cout << "Number\n" : cout << "Not number\n"; return EXIT_SUCCESS; }
Use find_first_not_of
Method to Determine if a String Is a Number
This version utilizes a built-in string
search algorithm. The algorithm searches for the first character equal to none of the characters in string
passed as an argument (in our case - "0123456789"
). If the character is not found, string::npos
is returned, thus we return the result of comparison from isNumber
.
#include <iostream> using std::cout; using std::cin; using std::endl using std::string; bool isNumber(const string& str) { return str.find_first_not_of("0123456789") == string::npos; } int main(){ string str1 = "231524randstr23"; string str2 = "23152423"; string str3 = "a3152423"; isNumber(str1) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str2) ? cout << "Number\n" : cout << "Not number\n"; isNumber(str3) ? cout << "Number\n" : cout << "Not number\n"; return EXIT_SUCCESS; }
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.
Related Article - C++ String
How to Check if String Is a Number C++
Source: https://www.delftstack.com/howto/cpp/how-to-determine-if-a-string-is-number-cpp/