Thread: Reading whole string

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Reading whole string

    Hi guys,

    This program is supposed to count string length from the whole file, then count number of words and finally count average word length of all the words in file.

    I seem to be having a problem with reading every line from a text file. So, the problem is that: it counts the words correctly, but length is being counted only from the first line of text file and it's being counted with every space and symbol.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define EIL 80
    
    int main()
    {
        FILE *file;
        char delim[] = " ,.!:;?\n";
        char line[EIL];
        char *words, *ptr;
        int ch;
        float length, word_count=0, words_average=0;
    
        file = fopen ("file.txt", "r");
    
        if (file==NULL)
            printf ("\nError\n\n");
        else
        {
        while ((ch=fgetc(file)) !=EOF)
        {    fgets(line, EIL, file);
            ptr = line;
    
        if(words!=NULL)
                {
                    length=0;
                    words=strtok(ptr, "");
                    length=strlen(words);
                    length++;
                }
    
        while ((words = strtok(ptr, delim)) !=NULL )
              {
               word_count++;
               ptr = NULL;
              }
    
         }
            words_average=length/word_count;
    
             printf("Length: %.0f  \nCount: %.0f  \nAverage word length: %.2f\n\n", length, word_count, words_average);
            fclose(file);
        }
        system("pause");
        return 0;
    }
    Input file:
    Code:
     One ; Two Three : Four
    Five Six!
    Seven Eight
    Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    output screen isn't giving proper result
    here is a question that I had posted earlier. It may be helpful.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look in the documentation for strtok() ... I think you'll find you're not using it correctly.

    Also there's a much easier way to count words... using isspace() ... Basically you just count the spaces between the words...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  2. Java: Is reading String by String possible?
    By alphaoide in forum Tech Board
    Replies: 5
    Last Post: 08-28-2004, 07:01 PM
  3. reading a string
    By cheeves in forum C Programming
    Replies: 4
    Last Post: 11-20-2003, 06:35 PM
  4. Reading in an int followed by a string
    By ChwanRen in forum C Programming
    Replies: 4
    Last Post: 05-07-2003, 03:09 PM
  5. Reading a string
    By LavaLamp27 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2001, 06:27 PM