/*  Find2_c
      This program searches any file for a certain string
      that the user has input.  Every time the program
      finds the string in the file, it prints to a
      file the keyword and offsets of where the string started.
         The program is case insensitive.  All characters
      are converted to upper case.
         The algorithm does have some problems.  It will miss
      the string abc in ababc.
         The program expects a file called find2_log to exist
      on flp1_.  
*/

#include <stdio_h>

main() {
   char  c,
         string[20],
         file[30];

   int   count,
         file_count,
         str_len,
         fd,
         fd2;

/* abcdefghijk */

   count = 0;
   file_count = 0;

   printf("Enter File Name : \n");
   gets(file);

   printf("Enter Search String : \n");
   gets(string);

   fd = fopen(file,"r");
   if (fd == NULL)  {
          printf("Did not open file");
         abort(1);
   }

   fd2 = fopen("flp1_find2_log","a");

   fputs(string,fd2);
   fprintf(fd2,"\n   ");
   fputs(file,fd2);

   str_len = strlen(string);

   while (( c = getc(fd)) != EOF) {
      ++file_count;
      /*  make sure a character is upper case */
      if (isascii(c)) c=toupper(c);
      if ( c == toupper(string[count]))
            ++count;
      else
            count = 0;

      if (count == str_len) {
            printf(" String found at %d\n",file_count-str_len+1);
            fprintf(fd2,", %d",file_count-str_len+1);
      }
   }
   fprintf(fd2,"\n");
   fclose(fd2);

}

