differentiate a digit and a space.

This is a discussion on differentiate a digit and a space. within the C Programming forums, part of the General Programming Boards category; i have an array of numbers i.e.: 9 2938932 29382332 what i want to do is remove these spaces so ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    differentiate a digit and a space.

    i have an array of numbers i.e.: 9 2938932 29382332 what i want to do is remove these spaces so it can be joined. i have tried... isdigit(atoi(temp)) == 0) basically in this line of code temp is a char array in which i stored each individual number and apply atoi to convert it to a digit so i can use it with isdigit. Unfortunately it doesn't seem to work. When i put a space in the array i.e. ' ' it prints out a 0. any ideas?

  2. #2
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    nevermind i think i have solved this problem by isdigit(atoi(temp)) == 0) && string[i] != ' '

  3. #3
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Axel
    i have an array of numbers i.e.: 9 2938932 29382332 what i want to do is remove these spaces so it can be joined. i have tried... isdigit(atoi(temp)) == 0) basically in this line of code temp is a char array in which i stored each individual number and apply atoi to convert it to a digit so i can use it with isdigit. Unfortunately it doesn't seem to work. When i put a space in the array i.e. ' ' it prints out a 0. any ideas?
    You shouldn't be converting the strings to integers and then passing the integers to isdigit. I can see why you might think that if you've seen something like this:
    Code:
    #include <ctype.h>  
     int isdigit(int c);
    but the parameter is
    an integer whose value is representable as an unsigned char, or the value of the macro EOF
    You should just loop over the string passing each character to isdigit:
    Code:
    for (i=0; i<strlen(temp); i++) {
        if (!isdigit(temp[i])) {
            /* this is one of those spaces */
        }
    }
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks for the hints although


    You shouldn't be converting the strings to integers and then passing the integers to isdigit. I can see why you might think that if you've seen something like
    my situtation is a bit more complicated than that. i have a char array which is used by fgets. This has to be converted char by char. The char is then fed through atoi giving me an actual int where i can use isdigit

  5. #5
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Axel
    my situtation is a bit more complicated than that. i have a char array which is used by fgets. This has to be converted char by char. The char is then fed through atoi giving me an actual int where i can use isdigit
    it is an error to pass atoi() a char.

    atoi() must be passed a string.

    isdigit() must be passed a char.

    atoi() returns an integer created from a string of digits and that is going to be an inappropropriate value to pass to isdigit().
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Quote Originally Posted by Axel
    i have an array of numbers i.e.: 9 2938932 29382332 what i want to do is remove these spaces so it can be joined. i have tried... isdigit(atoi(temp)) == 0) basically in this line of code temp is a char array in which i stored each individual number and apply atoi to convert it to a digit so i can use it with isdigit. Unfortunately it doesn't seem to work. When i put a space in the array i.e. ' ' it prints out a 0. any ideas?
    If you want to make a single number out of those three (assuming you could hold that number in an int, which you can't) then you could:
    • Go over the whole string, removing characters that are not digits; or
    • Write your own function to convert digits to numbers. (It's easier than it sounds.)


    I don't think that's what you want to do, however, so I won't go into further detail.

    If you want to get three separate numbers from the string, use sscanf() or a combination of strtol and pointers or isdigit() and itoa() (in order of feasibility). Assuming you want to use sscanf();
    Code:
    sscanf("%d%d%d", &number_one, &number_two, &number_three);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21