Logo

Programming-Idioms

History of Idiom 137 > diff from v30 to v31

Edit summary for version 31 by arouene:
New Cpp implementation by user [arouene]

Version 30

2019-09-26, 16:13:08

Version 31

2019-09-27, 12:28:34

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

Imports
#include <algorithm>
#include <cctype>
#include <string>
Code
bool b = false;
if (! s.empty() && std::all_of(s.begin(), s.end(), [](char c){return std::isdigit(c);})) {
    b = true;
}
Comments bubble
s must be a std::string
Demo URL
http://cpp.sh/2jbql