/*  Find_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 the
      screen the offset 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.
*/

#include <stdio_h>

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

   int   count,
         file_count,
         str_len,
         fd;

/* 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);
   }

   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);

   }
}

