# Thread: differentiate a digit and a space.

1. ## 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. nevermind i think i have solved this problem by isdigit(atoi(temp)) == 0) && string[i] != ' '

3. 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 */
}
}```

4. 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. 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().

6. 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);`