Thread: Problem with isdigit()

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Question Problem with isdigit()

    I am writing a spell checker.. I have already written it to ignore words that are ALL CAPS.

    Code:
    int allupper(char* word)
    {
        int i = 0;
        
        while(word[i] != NULL)
        {
            if(isupper(word[i]) == 1)
                i++;
            else
                return 0;
        }
        return 1;
    }
    This worked fine, thought I could do the same thing with isdigit() to have it ignore numbers, but couldn't get it to work...

    Code:
    int isnumber(char* word)
    {
        int i = 0;
        
        while(word[i] != NULL)
        {
            if(isdigit(word[i]) == 1)
                i++;
            else
                return 0;
        }
    
        printf("Return 1\n");
        return 1;
    }
    Can anyone tell me what's wrong? Also, is there a way to loop through the string using the Null Terminator as an ending point, I tried while(strcmp("\0", word[i]) != 0) but didn't work right.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think something is wrong with it?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    It seems like it should work, but it won't even recognize word[0] as a digit even if it is, let alone the rest. I don't know why.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This code:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>
    
    int isnumber(char* word)
    {
        int i = 0;
    
        while(word[i] != '\0')
        {
            if(isdigit(word[i]) == 1)
                i++;
            else {
                printf("word[&#37;d] gives %d.\n", i, isdigit(word[i]));
                return 0;
            }
        }
    
        printf("Return 1\n");
        return 1;
    }
    
    int main(void) {
        char foo[] = "15";
        int bar = isnumber(foo);
        printf("bar is %d.\n", bar);
        return 0;
    }
    prints
    Code:
    word[0] gives 4.
    bar is 0.
    on my (Win32) system. I'm guessing you should reexamine your idea of "true" in C.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Thanks, I remember reading it returns non-zero, didn't know it returns 48-57 for digits 0-9 respectively, set it to != 0 and I was on my way!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with isdigit()
    By Mastrchief in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 02:19 PM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM