Determine if a string is numeric: Difference between revisions

Content added Content deleted
(→‎BQN: add)
(added C translation to C++)
Line 1,531: Line 1,531:


<syntaxhighlight lang="c">#include <ctype.h>
<syntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdlib.h>

int isNumeric (const char * s)
bool isNumeric(const char *s) {
{
if (s == NULL || *s == '\0' || isspace(*s))
if (s == NULL || *s == '\0' || isspace(*s)) {
return 0;
return false;
char * p;
}
strtod (s, &p);
char *p;
strtod(s, &p);
return *p == '\0';
return *p == '\0';
}</syntaxhighlight>
}</syntaxhighlight>
Line 1,572: Line 1,574:


=={{header|C++}}==
=={{header|C++}}==
{{trans|C}}<syntaxhighlight lang="cpp">#include <cctype>
#include <cstdlib>

bool isNumeric(const char *s) {
if (s == nullptr || *s == '\0' || std::isspace(*s)) {
return false;
}
char *p;
std::strtod(s, &p);
return *p == '\0';
}</syntaxhighlight>

Using stringstream:
Using stringstream:
<syntaxhighlight lang="cpp">#include <sstream> // for istringstream
<syntaxhighlight lang="cpp">#include <sstream> // for istringstream