How to use isdigit function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pete ritter
    New Member
    • Dec 2010
    • 4

    How to use isdigit function?

    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.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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.
    Code:
    int j, k;
    char i;
    char *student;
    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?
    Last edited by donbock; Dec 3 '10, 01:04 PM. Reason: Numbered the questions so I can refer to them in later replies.

    Comment

    • pete ritter
      New Member
      • Dec 2010
      • 4

      #3
      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:
      Code:
      int main(int argc, char *argv[])
      {
      	
             	char   student [100];
             	char num [100];
             	int con [100];
             	char i = 0;
             	
             	int k,m,j,n = 0;
           
             
             	
             
             	
             	FILE * fp = fopen(argv[1],"r");
             	
      	
      	while(!feof(fp))
      	{
      	fgets(student,100,fp);
      	
      	}
      	
      	
      	
      	for(k=0;k<10;k++)
      		
      	{
      		i = student [k];
      		
      		if( isdigit(i)){
      			
      			num [j] = i;
      			j++;
      		}
      		
      		
      		
      			
      		
      	
      	
      
      	}
      		printf("%s",num);
      and it is still printing weird characters, is there something wrong with my arrays?
      Thanks,

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          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.
          Last edited by donbock; Dec 4 '10, 04:29 AM. Reason: Dragged Banfa into this. Later added issue 13 (fgets).

          Comment

          • pete ritter
            New Member
            • Dec 2010
            • 4

            #6
            Would I want to assign the null at the end of the array, something like this.
            Code:
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
            #include <ctype.h>
            
            int main(int argc, char *argv[])
            {
            	
                   	char student [100];
                   	char num [100];
                   	int con [100];
                   	char i = 0;
                   	
                   	int k,m,j,n = 0;
                 
                   
                   	
                   
                   	
                   	FILE * fp = fopen(argv[1],"r");
                   	
            	
            	while(!feof(fp))
            	{
            	fgets(student,100,fp);
            	
            	
            	
            	
            	
            	for(k=0;k<100;k++)
            		
            	{
            		
            		i = student [k];
            		
            		if( isdigit(i)){
            			
            			num [j] = i;
            			j++;
            			
            		}
            		
            		
            		
            		
            	}	
            		
            			
            		
            	
            	
            	num[j] = '\0';
            
            	
            		printf("%s",num);

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              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).

              Comment

              • pete ritter
                New Member
                • Dec 2010
                • 4

                #8
                what should I initialize it as? a pointer?

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  I mean the initial value of j -- the value you want j to have the first time you get to line 39.

                  Comment

                  Working...