title: How to count the occurrences of a specific string in a text file using C
body: <p>I am trying to figure out how to count the number times a specific string "test" occurs in a text file using C programming. I want the the program to display the final count upon completion.</p>
  
  <p>This is the code I have come up with, but it doesn't seem to do to the trick. The count I generate is slightly lower than what is actually present in the text file.</p>
  
  <p>Does anyone see what I'm doing wrong? I'm fairly new to C programming, so any insight would be greatly appreciated!</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  int main()
  {
      FILE *ptr_file;
      char buf[200];
      char key[] = "test"; // the string I am searching for
      int wordcount = 0;
  
      ptr_file = fopen("input.txt","r"); // my input text file
  
      while (fgets(buf,200, ptr_file)!=NULL)
      {
          if((strstr(buf,key)) !=NULL){
              wordcount++;
          }
      }
      fclose(ptr_file);
      printf("%d",wordcount);
  }
  </code></pre>