By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

How to use isdigit function?

P: 4
Hi,

I am trying to use the isdigit function to extract digits from an array containing a string file. What I am wondering is if the variable used in it can be a char?

this is what I have,

for(k=0;k<10;k++)

{
i = student [k];

if( isdigit(i))

num [j] = i;

j++;

printf("%s",num);

}

For some reason it is printing weird characters repetivaly as if it is not looping through the whole student array.

any help is greatly appreciated.
Dec 3 '10 #1

✓ answered by donbock

Questions #1 and #5 from my earlier reply still apply. (Banfa has reiterated question #5.)

Questions #2 and #3 are addressed by hard-coding the size of the arrays (magic number = 100) and hard-coding the maximum number of characters to look at in each line (magic number = 10). This is a brittle solution.

New issues raised by your new program:
  1. still applies
  2. brittle solution
  3. brittle solution
  4. fixed
  5. still applies
  6. fixed
  7. Initializing i on line 7 serves no purpose. This is not a big deal because it does no harm either.
  8. You don't check if fopen failed on line 15.
  9. You have a tight while loop in lines 18-22 for reading the input file into the student array. The loop is so tight that you discard all but the last line of the file.
  10. j is not initialized when you first use it on line 33. It starts at some totally random value.
  11. Were you to fill num independently for each line of the input file (see #9 above), j is not reset when you start the next line (because you don't initialize it, see #10 above). This means that the digits for all lines in the file would be written sequentially to num. You would almost certainly write past the end of the num array.
  12. It is not uncommon for stdout to be buffered. If it were, then your printf on line 45 would yield no apparent output. Output characters are withheld (buffered) until a newline character is sent to the output. Whether or not stdout is buffered is implementation dependent -- just because it seems to work ok for your compiler doesn't mean the behavior wouldn't be different on another compiler.
  13. You don't check if fgets failed on line 20.

Share this Question
Share on Google+
8 Replies


Expert 100+
P: 2,396
In the future, please use CODE tags around code snippets.

You didn't tell us the types of the variables. I will assume the following; please correct me if I guessed wrong.
Expand|Select|Wrap|Line Numbers
  1. int j, k;
  2. char i;
  3. char *student;
  4. char num[...];
  1. What if the student string doesn't fill the student array? For example, char student[10] = "bill";
  2. How do you know you don't read past the end of the student array?
  3. How do you know the num array is large enough to hold all the digits you extract from student?
  4. You increment j whether the character is a digit or not.
  5. The num string is not null-terminated.
  6. You print the num string after every character in student. Wouldn't you rather wait and print it once after you're done with all the characters in student?
Dec 3 '10 #2

P: 4
Yes you were right in geussing the types except for student, i made student an array that the file is being read into. Also i apologize for not using the code tags.

I changed it around a bit and this is what I have now:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.  
  4.            char   student [100];
  5.            char num [100];
  6.            int con [100];
  7.            char i = 0;
  8.  
  9.            int k,m,j,n = 0;
  10.  
  11.  
  12.  
  13.  
  14.  
  15.            FILE * fp = fopen(argv[1],"r");
  16.  
  17.  
  18.     while(!feof(fp))
  19.     {
  20.     fgets(student,100,fp);
  21.  
  22.     }
  23.  
  24.  
  25.  
  26.     for(k=0;k<10;k++)
  27.  
  28.     {
  29.         i = student [k];
  30.  
  31.         if( isdigit(i)){
  32.  
  33.             num [j] = i;
  34.             j++;
  35.         }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.     }
  45.         printf("%s",num);
and it is still printing weird characters, is there something wrong with my arrays?
Thanks,
Dec 3 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
You are treating num as if it is a c style string. However c style strings have a 0 ('\0' or NUL) terminating character and you never put that zero terminator into the array num so it is never really a c style string just an array of char.

And since it is not a c style string use %s in printf shows the wrong results.
Dec 3 '10 #4

Expert 100+
P: 2,396
Questions #1 and #5 from my earlier reply still apply. (Banfa has reiterated question #5.)

Questions #2 and #3 are addressed by hard-coding the size of the arrays (magic number = 100) and hard-coding the maximum number of characters to look at in each line (magic number = 10). This is a brittle solution.

New issues raised by your new program:
  1. still applies
  2. brittle solution
  3. brittle solution
  4. fixed
  5. still applies
  6. fixed
  7. Initializing i on line 7 serves no purpose. This is not a big deal because it does no harm either.
  8. You don't check if fopen failed on line 15.
  9. You have a tight while loop in lines 18-22 for reading the input file into the student array. The loop is so tight that you discard all but the last line of the file.
  10. j is not initialized when you first use it on line 33. It starts at some totally random value.
  11. Were you to fill num independently for each line of the input file (see #9 above), j is not reset when you start the next line (because you don't initialize it, see #10 above). This means that the digits for all lines in the file would be written sequentially to num. You would almost certainly write past the end of the num array.
  12. It is not uncommon for stdout to be buffered. If it were, then your printf on line 45 would yield no apparent output. Output characters are withheld (buffered) until a newline character is sent to the output. Whether or not stdout is buffered is implementation dependent -- just because it seems to work ok for your compiler doesn't mean the behavior wouldn't be different on another compiler.
  13. You don't check if fgets failed on line 20.
Dec 3 '10 #5

P: 4
Would I want to assign the null at the end of the array, something like this.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.  
  9.            char student [100];
  10.            char num [100];
  11.            int con [100];
  12.            char i = 0;
  13.  
  14.            int k,m,j,n = 0;
  15.  
  16.  
  17.  
  18.  
  19.  
  20.            FILE * fp = fopen(argv[1],"r");
  21.  
  22.  
  23.     while(!feof(fp))
  24.     {
  25.     fgets(student,100,fp);
  26.  
  27.  
  28.  
  29.  
  30.  
  31.     for(k=0;k<100;k++)
  32.  
  33.     {
  34.  
  35.         i = student [k];
  36.  
  37.         if( isdigit(i)){
  38.  
  39.             num [j] = i;
  40.             j++;
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.  
  47.     }    
  48.  
  49.  
  50.  
  51.  
  52.  
  53.     num[j] = '\0';
  54.  
  55.  
  56.         printf("%s",num);
Dec 4 '10 #6

Expert 100+
P: 2,396
That is a fine way to null-terminate the string ... but only if you initialize j before using on line 39 (see issue #10 above).
Dec 4 '10 #7

P: 4
what should I initialize it as? a pointer?
Dec 4 '10 #8

Expert 100+
P: 2,396
I mean the initial value of j -- the value you want j to have the first time you get to line 39.
Dec 4 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.