Thursday, April 5, 2012

Searching WORD and its COUNT in a FILE - C program


string Searching in a FILE- C program
count of the appearances 
/* program for searching a keyword in a FILE.
   program also tells the number of matches */


 #include "stdio.h"  
 #include "string.h"
 #include "ctype.h"

 void main()
 {
   FILE *fp;
   char keyword[15]={0},c;
   int i,length,count=0;
   clrscr();

   fp=fopen("cfile.txt","r"); //you can give any file you wish
   printf("enter keyword to search in the file-");
   scanf("%s",keyword);
   length=strlen(keyword);

   while((c=tolower(getc(fp)))!=EOF)
     {
       for(i=0;(c==keyword[i]);i++)
       {
      c=tolower(getc(fp));
       if(i==(length-1))
        count++;
       }
     }
   printf("there are %d matches",count);
     fclose(fp);
   getch();
 }

No comments:

Post a Comment