C Program to check whether a given word exists in a file or not , If yes then find the number of times it occurs

 


// Program by Akash Tripathi (@proakash256) 
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
    FILE *filepointer;
    char *s1;
    s1 = malloc(1024 * sizeof(char));
    char *word;
    word = malloc(1024 * sizeof(char));
    printf("Enter the word which you want to Search : ");
    scanf("%s"word);
    fflush(stdin);
    word = realloc(wordstrlen(word) + 1);
    int i = 0c = 0;
    filepointer = fopen("input.txt""r");
    do
    {
        if (feof(filepointer) != 0)
            break;
        s1[i] = fgetc(filepointer);
        i = i + 1;
    } while (1);
    s1 = realloc(s1strlen(s1) + 1);
    fclose(filepointer);
    char s2[10];
    for (int i = 0 , j = 0s1[i] != '\0'i = i + 1)
    {
        if (s1[i] == ' ' || s1[i] == '.')
        {
            if (strcmp(s2 , word) == 0)
                c = c + 1;
            s2[0] = '\0';
            j = 0;
        }
        else
        {
           s2[j] = s1[i];
           j = j + 1;
        }
    }
    printf("\nThe word %s appears %d times.\n\n"wordc);
    free(s1);
    free(word);
    return 0;
}

Comments