Program about counting how many times a word appeared in a txt file

This is a discussion on Program about counting how many times a word appeared in a txt file within the C Programming forums, part of the General Programming Boards category; Hello, everyone. I am a beginner of c programming. I try to write a program to count how many times ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Question Program about counting how many times a word appeared in a txt file

    Hello, everyone. I am a beginner of c programming. I try to write a program to count how many times a word appeared in a txt file. However, i find it stops when there is a new paragraph and it only return the number of the words in the first paragrah but not the whole text. Can anyone tell me what is going wrong in my program?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 1000
    
    char *search(char *, char *);
    
    int main()
    {
        char string1[SIZE], string2[SIZE], oriword[30], 
             *newword, file1[30];
        int  count;
        FILE *inFile;
        
        printf("Please enter an input file name: ");
        scanf("%s", &file1);
        inFile = fopen(file1, "r");
        printf("\nWhich word do you want to find: ");
        scanf("%s",&oriword);
        while (fgets(string1, SIZE, inFile) != NULL);
        newword = search(string1, oriword);
        count = 0;
        while (newword != NULL)
        {
              count++;
              newword = search(newword + strlen(oriword), oriword);
              printf("%s\n", newword);
        }
              
        printf("\nThe number of %c%s%c appeared %d time(s).\n", '"', oriword, '"', count);  
        fclose(inFile);
        fflush(stdin);
        printf("\nPlease press the Enter key to continue......\n");
        getchar();
        return 0;
    }
    
    
    char *search(char *string3, char *owd)
    {
         int j, k;
         char *tmp;
         
         j = strlen(string3);
         k = strlen(owd);
         for (tmp = string3; tmp <= string3 + j - k; tmp++)
         {         
              if ((tmp == string3 || *(tmp - 1) == ' ' || *(tmp - 1) == '.')
              && strncmp(tmp, owd, k) == 0 
              && (*(tmp + k) == ' ' || *(tmp + k) == '.' || *(tmp + k) == '\0'))
                  return tmp;
         }
         return NULL;
    }
    thank you very much

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    First things first, don't use scanf to read strings. Use fgets or read up on format specifications.
    fflush(stdin) is undefined, read FAQ.
    search does the same as strstr, from what I can tell. Why reinvent the wheel?
    Further, we take a look at this line:
    Code:
    while (fgets(string1, SIZE, inFile) != NULL);
    It reads the text from the file into the same buffer over and over again. Your previous data in the buffer is lost, so of course it only finds the words in one line.
    So you have two solutions:
    Read the entire file into one big, humongous array or read a line, parse it for the number of words that occurs and loop.
    Last edited by Elysia; 12-02-2007 at 04:14 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    thank you elysia, I know what should i do now. Thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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