counting words in a text file...

This is a discussion on counting words in a text file... within the C Programming forums, part of the General Programming Boards category; what is the best way to count the number of words in a text file...excluding white space, end of lines, ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question counting words in a text file...

    what is the best way to count the number of words in a text file...excluding white space, end of lines, and end of file?
    The problem I'm having is that when I do my word count I always come up with more words than there are in the file.
    I have attached my code followed by a sample text file.

    Thanks!
    Flight
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    sorry here is the source code attachment

    attached is the source code file.
    Attached Files Attached Files

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >what is the best way to count the number of words in a text file... ?

    Take a look at this. And then check out the followups.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Wink nice try no cookie...

    thanks dave..I looked at it tried what was suggested and still got the wrong answer...zero words counted s/b 7 words.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a sample of your latest code if you're still having trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I looked at it tried what was suggested and still got the wrong answer

    In the followup, it was suggested that isalpha should be replaced with !isspace. Doing so will result in 6 words being found in your file. The reason it is not 7 is because your file does not end in a newline.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    here's my code...

    the code works well except it will not count the last word in the file. I noticed the last word doesn't have a line feed at the end. How can I fix this?
    Thanks!
    flight




    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
    	//local declarations
    	char file_name[100] ;
        int count ;
    
        int update_counter = 0;
    
        FILE *inp1, *out1 ;
    
    	// init some stuff
        count=0 ;
    
       // prompt the user
        printf("Enter Filename : ")  ;
    
    	// read the keyboard input
        fgets(file_name, sizeof(file_name), stdin);
    
    	//remove the newline character from the end of the input filename
    	file_name[strlen(file_name) - 1] = '\0' ;
    
    	// open input file
    	inp1 = fopen(file_name, "r") ;
        if(inp1 == NULL)	
    	{
    	 fprintf(stderr, "can't open %s\n", file_name) ;
    	 exit(EXIT_FAILURE) ;
    	}
    
    	// open output file
    	out1 = fopen("output.txt", "w") ;
        if(out1 == NULL)	
    	{
    	 fprintf(stderr, "can't open %s\n", "output.txt") ;
    	 exit(EXIT_FAILURE) ;
    	}
    
        // count the words in the input file
        for ( ;; )
        {
           int ch = fgetc(inp1);
           if ( ch == EOF )
           {
              break;
           }
           if ( isalpha(ch) || isdigit(ch) || ispunct(ch))
           {
              update_counter = 1;
    
           }
           if ( isspace(ch) && update_counter )
           {
              count++;
              update_counter = 0;
           }
        }
     
    	//echo the word count for the file
        printf("\nFile %s contains %d words \n\n", file_name, count) ;
     
    	//write the word count to the output file
    	fprintf(out1,"File %s contains %d words \n", file_name, count) ;
    
    	// we're done...close files
    	fclose(inp1) ;
    	fclose(out1) ;
    
      return 0;

  8. #8
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    it's because the word counter is increased when a white space is found, if the file ends with a letter, the program won't find a white space and thus won't increase the counter.
    To fix this, add this code after the for loop and before printing the result:

    Code:
    if (update_counter) {
    	count++;
    }

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    it worked but...

    would you consider this an unelegant method? Don't get me wrong I sincerely do appreciate your reply!

  10. #10
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    yep, I had a similar idea when I posted, the elegant way IMHO is adding this piece of code before the break statement in the for loop.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    hammer...what are your thoughts...

    Thanks again!
    flight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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