scanning string for ![0..9]

A

aurgathor

Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}
 
P

pete

aurgathor said:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

There's another way.
I don't know if it's better.
// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}

#include <ctype.h>

for (count = 0; str[count] != '\0'; ++count) {
if (!isdigit((unsigned char)str[count])) {
return INVALID_CHAR;
}
}
 
A

Alex Fraser

aurgathor said:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}

Alternative, debatable if it's better:

#include <ctype.h>

char *p = str;
while (*p)
if (!isdigit((unsigned char)*p++)) return INVALID_CHAR;

Alex
 
C

CBFalconer

aurgathor said:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}

Don't use // comments in newsgroups. They don't survive line
wrapping very well, and are illegal in the most prevalent standard,
C90.

#include <ctype.h>
...
int digitsonly(char *str) {
while (*str)
if (!(isdigit(unsigned char)*str++)) return INVALID_CHAR;
return ALL_DIGS;
}
 
A

Andrey Tarasevich

aurgathor said:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}

It depends on what you mean by "better". Shorter? Faster?


int count = 0;
sscanf(str, "%*[0123456789]%n", &count);
if (str[count] != '\0)
return INVALID_CHAR;
 
S

Stephen Sprunk

aurgathor said:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}

Take a look at isdigit(). Be sure to #include <ctype.h>.

S
 
C

Chris Torek

strcspn(str, "0123456789")

This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.

A somewhat twisted method is to use strtol or strtoul in combination
with strspn or strcspn to check for leading whitespace and/or sign:

int has_nondigit;
char *ep;

has_nondigit = strcspn(str, "0123456789") != 0 ||
(strtoul(str, &ep, 10), *ep != '\0');
 
E

Eric Sosman

Chris said:
This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.

A somewhat twisted method is to use strtol or strtoul in combination
with strspn or strcspn to check for leading whitespace and/or sign:

int has_nondigit;
char *ep;

has_nondigit = strcspn(str, "0123456789") != 0 ||
(strtoul(str, &ep, 10), *ep != '\0');

A possibly simpler approach that more closely matches
the problem statement might be

has_nondigit = str[ strspn(str, "0123456789") ] != '\0';

At about this point, though, I think I'd take a step
back and ask why the O.P. wants to determine whether a
string consists only of digits. If the intent is to convert
to a number, it's probably simpler to go ahead and convert
with strtol() or strtoul() and check that it succeeded and
converted the entire string; the conversion will detect and
report any non-digit it encounters.

Sometimes (not always, but sometimes) solving the stated
problem is less useful than coming up with a better statement.
 
T

Tor Rustad

Chris Torek said:
This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.

True, if the lenght of digits is wanted

strspn(str, "0123456789")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top