Thread: isdigit

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    isdigit

    Is the function isdigit able to find digits that are in a string?
    For example if I had a character string named string that contained the characters 34678904.

    would
    Code:
    isdigit(string)
    return a 0 since the character is a digit or would it return a nonzero b/c it recognizes it as something else?

    If it returns a nonzero, what is a good way to check if a string is made up of characters or digits?

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    isdigit returns true if a character is one of 0123456789. You can check to see if a string consists solely of digits by looping over every element:
    Code:
    int anumber(char *s) {
      while (*s != '\0') {
        if (!isdigit(*s++))
          return 0;
      }
    
      return 1; 
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Is the function isdigit able to find digits that are in a string?

    Yes.

    >would
    > isdigit(string)
    >return a 0

    No. The function isdigit is used on a single character, not a string.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       const char text[] = "34678904 plus other stuff !5@#$";
       size_t i;
       for ( i = 0; i < sizeof text - 1; ++i )
       {
          if ( isdigit(text[i]) )
          {
             putchar(text[i]);
          }
       }
       putchar('\n');
       return 0;
    }
    
    /* my output
    346789045
    */
    >what is a good way to check if a string is made up of characters or digits?

    If this is in the process of trying to convert a string representation of a number into an integer value, this may help.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    thnaks guys you have been a big help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit woes
    By kryonik in forum C Programming
    Replies: 3
    Last Post: 10-14-2005, 05:10 PM
  2. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 06:03 AM
  4. #include cctype and the isdigit
    By nextus in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 07:55 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM