Easier way to determine if a string is an alphanumeric number?

F

fooboo

Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp)){
return 1
}
return 0;

Thanks
 
D

David Resnick

fooboo said:
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp)){
return 1
}
return 0;

Thanks


What is an alphanumeric number? isalnum means is a digit or is a
letter.
Do you mean isdigit or isxdigit?

Anyway, that way seems fine -- the code is clear and simple.
Why do you want an easier way? Taking strlen of temp in
the loop guard risks bad performance. The compiler COULD
know that isalnum won't change temp and optimize it out,
but it could also compute the length of temp every time
through the loop.

If you want a different way, here are some options:
1) If it won't be a ton of digits, use strtol (or strtoll).
Those have slightly different semantics than you
describe, in that they will eat leading whitespace
and accept +/- signs. Perhaps you don't want to
accept those, but you should be at least aware of
the issues.

2) Or you could do this:
if (strspn(temp, "0123456789") == strlen(temp)) {
return 0;
}
else {
return 1;
}

-David
 
M

Mark

fooboo said:
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp)){
return 1
}
return 0;


Making an assumption (based on returns) that you're writing a test
function...

int
test_isalnum(char *s)
{
while(isalnum(*s))
++s;
return *s == 0;
}

That should do the trick ;)
Mark
 
M

Mark

Mark said:
fooboo said:
Does anyone know if a easier way (built in function, or something) that
can verify that a string is an alphanumeric number? Here is what I am
doing now:

for(i=0; i < strlen(temp); i++){
if(!isalnum(temp)){
return 1
}
return 0;


Making an assumption (based on returns) that you're writing a test
function...

int
test_isalnum(char *s)
{
while(isalnum(*s))
++s;
return *s == 0;
}

That should do the trick ;)
Mark


Albeit my return value is opposite that of the original code...
if you want to return the opposite, change the return statement to:
return *s != 0;
though my preference would be to see it return true appropriately!

Mark
 
F

fooboo

What is an alphanumeric number? isalnum means is a digit or is a
letter.
Do you mean isdigit or isxdigit?

your right, I mixed it up. isdigit() is what I should have used. My
program worked just by coincidence.

I was just wondering if there was I library function I was not aware of.
 
E

Eric Sosman

fooboo said:
your right, I mixed it up. isdigit() is what I should have used. My
program worked just by coincidence.

I was just wondering if there was I library function I was not aware of.

What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.

Note that the check for success is necessary anyhow;
the string "999999999999999999999999999999999999999999999"
contains only digits, but probably doesn't represent a valid
`long' value.

By the way, when you use isdigit() or any of the other
<ctype.h> functions on characters plucked from a string, be
sure to use this (somewhat non-intuitive) idiom:

if (isdigit( (unsigned char) temp )) ...

The `char' type can be signed, so a particular `char' value
might be negative -- but the only negative value acceptable
to isxxx() and toxxx() is EOF. Programmers who fail to heed
this advice write programs that work in the USA but suddenly
fail when taken to München or St Estèphe.
 
F

fooboo

What will be your next step after discovering that the
string consists entirely of digits?
The design spec requires that it be stored as a string, even though it
should always be a number, don't ask me why.
Note that the check for success is necessary anyhow;
the string "999999999999999999999999999999999999999999999"
contains only digits, but probably doesn't represent a valid
`long' value.
the program flow won't allow the string I'm testing to be anything
other than a string of length 5 anyway, so a long data type would work,
if it wasn't for the design spec.
By the way, when you use isdigit() or any of the other
<ctype.h> functions on characters plucked from a string, be
sure to use this (somewhat non-intuitive) idiom:
good tip, that would have never even occured to me :)
 
C

Charles M. Reinke

Eric Sosman said:
[snip]
What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.

According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?

Thanx,
Charles
 
F

fooboo

According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?

If you really want to know:
http://groups-beta.google.com/group...read/thread/852ca3575c5c0a02/7267df53c744f6ed
 
E

Eric Sosman

Charles said:
[snip]
What will be your next step after discovering that the
string consists entirely of digits? If the next step is
"convert the string to its numeric equivalent," you could
just eliminate the test altogether, attempt the conversion
with strtol() or strtoul() (or even strtod(), if you want
to handle floating-point numbers), and then check whether
the conversion succeeded.


According to the man pages on my machine if strtod (for example) fails errno
MAY be set to something, but it doesn't seem to be required : "If no
conversion could be performed, 0 is returned and errno may be set to
EINVAL." So how can you verify that it succeeded? Is this specified in the
Standard (I don't have a copy handy), or it implementation specific and thus
non-portable?

You've got to look at two different kinds of failure: input
that doesn't have numeric form, and input that has correct form
but is out of range. To detect the first kind, use the second
argument to these functions, which (if non-NULL) designates a
`char*' variable that will receive a pointer to the first input
character that was not converted -- if no conversion could be
performed at all, this will be a pointer to the start of the
input string. If the input has the correct form but the result
is out of range, the function always sets errno to ERANGE. So
the whole dance goes something like this:

char *input = ...;
char *end;
double value;

errno = 0; /* in case it was ERANGE already */
value = strtod(input, &end);
if (end == input)
not_convertible();
else if (errno == ERANGE)
out_of_range();
else
successful_conversion();

If you want to require that the input consist only of a number
(not a number followed by other stuff), you could add a further
test for *end == '\0' -- that's up to you.

Sounds like a lot of tests, but in practice one usually lumps
all the error cases together, to get something like

errno = 0;
value = strtod(input, &end);
if (end == input || *end != '\0' || errno == ERANGE)
some_kind_of_error();
else
successful_conversion();

.... which isn't really all that daunting.

There are certainly more direct ways to answer exactly the
question the O.P. asked: "Does this string consist entirely of
digits?" The only reason I mentioned strtoxxx() is because the
next step after discovering that a string is composed of digits
is often to try to extract the value they represent; since
strtoxxx() will do the necessary checking anyhow, it may be
simpler just to attempt the conversion and see what happens.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top