Thread: Counting the characters from each word from a text file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    Counting the characters from each word from a text file

    Im trying to read off an input text file that reads ie. "Three happy dogs eat." My goal is to count the total characters and print ONLY that to the screen AND create an output txt file to read like this:

    Three 5
    happy 5
    dogs 4
    eat 3

    I have counted the total characters and created the output file but haven't counted the individual characters of each word.

    so my current output file is:

    Three
    happy
    dogs
    eat

    How can i count and put the number of characters for each word?

    Code:
    /* Include Files */
    #include <stdio.h>
    #include <ctype.h>                                     
    #include <stdlib.h>
    
    #pragma warning(disable:4996)  
    /*that will stop the warning on scanf being deprecated */
    
    /*fx prototypes*/
    int countAll ();
    int getstuff ();
    
    int main( void )
    {
    	
    	getstuff ();
    
    	countAll();
    
     return 0;
    }
    
    /***** fxs *****/
                        
    
    int getstuff( void )
    {
          FILE *infile;
          FILE *outfile;
          int iochar;
          int num = 0;
    
    			outfile = fopen("outtext.txt", "w");
    
         if (( infile = fopen( "cscdata.txt", "r" )) == NULL ) {
              printf( "text couldn't be opened\n" );
              exit( 1 );
         }
    
         while ( !feof( infile )) {                            
                                                           
              while ( isalpha( iochar = getc( infile )))
    
                      putc( iochar, outfile );
    
    		  putc( '\n', outfile );
    
                     ungetc( iochar, infile );                        
                                                          
              while ( isdigit( iochar = getc( infile )))
           
                    putc( iochar, outfile );
            
                    putc( '\n', outfile );
             
                    ungetc( iochar, infile );                       
                                                           
              while ( !isalpha( iochar = getc( infile )) && ( !isdigit( iochar )) && ( !feof( infile )))
              ;
              ungetc( iochar, infile );                        
         }
            
             fclose( infile );
    
    	 fclose (outfile);
    
         return 0;
    
    	 }
    		
    
    int countAll(void)
    {
    	FILE *infile;
    	int c = 0;
    	int count = 0;
    
    	infile = fopen("cscdata.txt", "r");
    	
    	while (c != EOF) 
    	{
    	count++;
    	c = getc(infile);
    	} 
    	
    	printf("number of all characters = %d\n", count);
    	
    	fclose (infile);
     	
    	return 0;
    
    }
    I was thinking of using strlen()? like

    Code:
     while ( isalpha( iochar = getc( infile )))
    
                      putc( iochar, outfile );
    
                      num = strlen( iochar);
                      putc (num, outfile);
    
    		  putc( '\n', outfile );
    
                     ungetc( iochar, infile );
    can anyone point me in the correct way?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    while ( !feof( infile )) {                            
                                                           
              while ( isalpha( iochar = getc( infile )))
    
                      putc( iochar, outfile );
    
    		  putc( '\n', outfile );
    
                     ungetc( iochar, infile );                        
                                                          
              while ( isdigit( iochar = getc( infile )))
           
                    putc( iochar, outfile );
            
                    putc( '\n', outfile );
             
                    ungetc( iochar, infile );                       
                                                           
              while ( !isalpha( iochar = getc( infile )) && ( !isdigit( iochar )) && ( !feof( infile )))
              ;
              ungetc( iochar, infile );                        
         }
    to

    Code:
    #include <string.h>
    
    #define MAXWORD  1000   /* maximal length of a word */
    ...   
        char word[MAXWORD];
    
        unsigned long count;
    
        for (count = 0; fscanf(infile, "%s", word) == 1; )
            count += fprintf(outfile, "%s %d\n",
                         word, strlen(word));
        if (count == 0)
            printf("no chars\n")
        else
            printf("number of all characters = %lu\n", count);
    ...
    Last edited by c.user; 04-26-2009 at 07:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    you may use more functions (for get word, for check count, for print message to the screen)

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    dat was cool.

    I forgot to mention though that punctuation shouldn't be counted, as well as white spaces.

    if the data file was :

    Three "happy" dogs eat

    the quotes would be counted

    should we throw in an "isalpha" somewhere?
    Last edited by flipguy_ph; 04-26-2009 at 07:53 PM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    if getc() gets data character by character, could I jsut put a count after it and fprintf the count?

    Code:
     while ( isalpha( iochar = getc( infile )))
                           putc( iochar, outfile );
    
                           count++
                           fprintf(outfile,"&d", count);
    			   
    		       putc( '\n', outfile );
                   ungetc( iochar, infile );
    i didn't work, but is it possible to do something like this?

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    ok. Im still on it.

    adding count apparently didn't work. no matter where I put it. I get a consecutive number for each word: three 1 happy 2 dogs 3 ect...

    I've done fgets instead of getc. that only gave me a whole string by line.

    c.user 's idea was the closest thing, but it included punctuation marks if added to the input data file. I only want to count the alpha-numeric characters from each word. Seems that "fscanf" is too broad and takes in everything, including punctuation and whitespaces...

    I'm still trying if anyone wants to help out.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    #include <string.h>
    
    #define MAXWORD  1000   /* maximal length of a word */
    ...   
        char word[MAXWORD];
    
        unsigned long count;
    
        for (count = 0; fscanf(infile, "%s", word) == 1; )
            count += fprintf(outfile, "%s %lu\n",
                         word, CountLetDigLine(word));
        if (count == 0)
            printf("no chars\n")
        else
            printf("number of all characters = %lu\n", count);
    ...
    +

    Code:
    #include <stddef.h>
    
    size_t CountDigitsLine(const char *l);
    size_t CountLettersLine(const char *l);
    
    /* CountLetDigLine:  count how man letters (all case)
                         and digits in l */
    size_t CountLetDigLine(const char *l)
    {
        size_t n;
        
        if (l == NULL)
            return 0;
        n = 0;
        n += CountDigitsLine(l) + CountLettersLine(l);
        return n;        
    }
    
    /* CountDigitsLine:  count how many digits in l */
    size_t CountDigitsLine(const char *l)
    {
        size_t n;
        
        if (l == NULL)
            return 0;
        for (n = 0; *l != '\0'; l++)
            if (isdigit(*l))
                n++;
        return n;
    }
    
    /* CountLettersLine:  count how many letters in l (all case) */
    size_t CountLettersLine(const char *l)
    {
        size_t n;
        
        if (l == NULL)
            return 0;
        for (n = 0; *l != '\0'; l++)
            if (isalpha(tolower(*l)))
                n++;
        return n;
    }
    you can count only one time letters and digits in the word (these do it twice) by write one function which will do that in one if like: if (isalpha(tolower(*l)) || isdigit(*l)) n++; , but I like to make code more independent in its parts

    update:
    example for functions
    Last edited by c.user; 04-27-2009 at 06:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM