1

Program in C to count the frequency of a given word in a text file

I made this program with the purpose of counting the frequency of a given word in a text file, but in count the characters.

Need help to fix it.

#include <stdio.h>
#include <stdlib.h> 

int main()
{
  FILE * fptr;
  char ch, * word, * a;
  int i=0, p=0;

  word =(char *) malloc(25 * sizeof(char));
  fptr = fopen("text.txt", "r");

  if (!fptr)
  {
    printf("File not found. \n");
  }
  else
  {
    printf("Word: ");
    scanf("%s", word);

    while(word[p]!='\0')
    {
      p++;
    }

    a=(char *) malloc(p * sizeof(char));

    while (*(ch+a) != EOF)
    {
      *(ch+a) = getc(fptr);

      if (*(ch+a) == * word)
      {
        i++;
      }
    }
  }  
  if (i==0)
    printf("Word not found.\n");
  else
  {
    printf("Word found %d times.\n",i);
  }

  fclose(fptr);
  return 0;
}
  • 1
    please read why [Need help to fix it is not a question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – imbr May 30 '20 at 19:23
  • 1
    See: [Why you should NOT cast `malloc`](https://stackoverflow.com/q/605845/10851804) – NAND May 30 '20 at 20:09
  • `while (*(ch+a) != EOF)` that does this mean? – Ed Heal May 30 '20 at 22:08

1 Answers1

1

The bug in your code is that getc() only get one character into memory. SO you must NOT make this *(ch+a) == * word since ch has a value not an address. let ch='x' and let a=10 so *(ch+a)==*('x'+10) which would derefernce an address you didn't allocate.

This website implements countOccurancees function, which take a pointer to const char and a file pointer and return the number of word occurrences.

The strstr() helps finding a first occurrence of a word by returning a pointer to the beginning of the located sub‐string.

#define BUFFER_SIZE 100
int countOccurrences(FILE *fptr, const char *word)
{
  char str[BUFFER_SIZE];
  char *pos;

  int index, count;

  count = 0;

  // Read line from file till end of file.
  while ((fgets(str, BUFFER_SIZE, fptr)) != NULL)
    {
      index = 0;

      // Find next occurrence of word in str
      while ((pos = strstr(str + index, word)) != NULL)
        {
      // Index of word in str is
      // Memory address of pos - memory
      // address of str.
      index = (pos - str) + 1;

      count++;
        }
    }

  return count;
}

So in main function just make i=countOccurrences(fptr, word);

main should be look like

int main()
{
  FILE * fptr;
  char  * word;
  int i=0;

  word = malloc(25 * sizeof(char));//Do NOT cast
  fptr = fopen("text.txt", "r");

  if (!fptr)
    printf("File not found. \n");

  else
  {
    printf("Word: ");
    scanf("%s", word);
    i=countOccurrences(fptr, word);
  }  
  if (i==0)
    printf("Word not found.\n");
  else
    printf("Word found %d times.\n",i);

  fclose(fptr);
  return 0;
}
NAND
  • 663
  • 8
  • 22
  • Just a quick question what does the strstr do? – isak delante May 30 '20 at 23:06
  • @isakdelante `strstr` **returns the address of first occurrence of a substring** , i.e. let `char *x="my name is xyz`, using `char *address=strstr(x, "name")`, so `printf("%s\n", address)` would print : `name is xyz` – NAND May 30 '20 at 23:15
  • Not ta Aℓℓ, but keep in mind when dereferencing (accessing and address via *) you should use pointers, because they point to an address :) – NAND May 30 '20 at 23:45