Write a C program to count the of words in a file named "file.txt". [A word is a sequence of letters ending with a blank, or a tab, or an end of line marker or end of file or punctuation symbols such as ",", ".", "!" and "?".].

#include<stdio.h>

main()
{
    int count=0;
    char ch;
    FILE *fp;
    fp=fopen("file.txt","r");
    if(fp==NULL)
        printf("\nFile not found");
    else
    {
        do
        {
            ch=fgetc(fp);
            if(ch==' ' ||ch=='\n' || ch==',' || ch=='.' || ch=='?' || ch=='!' || ch=='\t' || ch==EOF)
            {
                count++;
            }
        }while(ch!=EOF);
       
    }
    printf("\nNumber of words=%d",count);
   
}

No comments:

Post a Comment