how to check if given c++ string or char* contains only digits?

Of course, there are many ways to test a string for only numeric characters. Two possible methods are:

bool is_digits(const std::string &str)
{
    return str.find_first_not_of("0123456789") == std::string::npos;
}

or

bool is_digits(const std::string &str)
{
    return std::all_of(str.begin(), str.end(), ::isdigit); // C++11
}

Leave a Comment