. Advertisement .
..3..
. Advertisement .
..4..
A string in C++ is used to store a big collection of characters, which include letters, alphabets, and any other character. All of them are between double quotes.
The following article will introduce some useful methods to check if a string is a number in C++.
How To Check If A String Is A Number In C++
Method 1: Use The std::isdigit() Function
Using the std::isdigit()
function is one of the most common methods to deal with the problem. Here, you will employ the function by passing a string as a parameter.
Then the std::isdigit()
will iterate all characters present in the string. If the function finds out the first non-digit, it tends to return false and display an according message.
On the other hand, the result will be true and display an appropriate message if there is no non-digit found.
Code:
#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 = "449521strcode";
string str2 = "643434981";
string str3 = "1643370001";
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
Number
Method 2: Use The std::isdigit And std::ranges::all_of Functions
Using the sole std::isdigit is ideal for the mighty and basic C++. If you are looking for a more eloquent solution, the std::ranges::all_of
can serve your needs.
The all_of()
option helps you avoid running a loop to check each item at once. Instead, it will check all properties on the element and return true for all satisfied ones.
Code:
#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 = "1643370001";
string str2 = "643434981";
string str3 = "449521strcode";
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:
Number
Number
Not number
Method 3: Use The find_first_not_of Method
This method focuses on using the built-in string search algorithm. In particular, it finds the first character, which is not equal to the other characters in the string. If it fails to find the character, it returns the string::npos and offers the comparison result from isNumber.
Code:
#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 = "1643370001";
string str2 = "643434981";
string str3 = "449521strcode";
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:
Number
Number
Not number
Method 4: Use The strtol() Function
The standard library strtol()
function is a great alternative to deal with the problem. It is used to parse a string in C++ as a specified base’s integral number. An impossible conversion will offer a zero value return.
In this function, use the std::string
to determine the numeric value of a string.
Code:
#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;
return 0;
}
Output:
true
Conclusion
Overall, there are numerous ways to check if a string is a number in C++. This article above has introduced you to four most simple and commonly used ones. Choose the one that suits your situation.
Leave a comment