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

Discussion in 'C Programming' started by fooboo, Jun 7, 2005.

  1. fooboo

    fooboo Guest

    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++){
    return 1
    return 0;

    fooboo, Jun 7, 2005
    1. Advertisements

  2. What is an alphanumeric number? isalnum means is a digit or is a
    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 Resnick, Jun 7, 2005
    1. Advertisements

  3. fooboo

    Mark Guest

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

    test_isalnum(char *s)
    return *s == 0;

    That should do the trick ;)
    Mark, Jun 7, 2005
  4. fooboo

    Mark Guest

    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, Jun 7, 2005
  5. fooboo

    fooboo Guest

    What is an alphanumeric number? isalnum means is a digit or is a
    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.
    fooboo, Jun 7, 2005
  6. fooboo

    Eric Sosman Guest

    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.
    Eric Sosman, Jun 7, 2005
  7. fooboo

    fooboo Guest

    What will be your next step after discovering that the
    The design spec requires that it be stored as a string, even though it
    should always be a number, don't ask me why.
    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.
    good tip, that would have never even occured to me :)
    fooboo, Jun 7, 2005
  8. 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

    Charles M. Reinke, Jun 7, 2005
  9. fooboo

    fooboo Guest

    According to the man pages on my machine if strtod (for example) fails errno
    If you really want to know:
    fooboo, Jun 7, 2005
  10. fooboo

    Eric Sosman Guest

    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)
    else if (errno == ERANGE)

    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)

    .... 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.
    Eric Sosman, Jun 7, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.