Advertisement
capncoolio

BinarySM search

Aug 21st, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. unsigned long stateMachineSearch (char *text, char *string) {
  5.     char c;
  6.     int start, offset = 0, i = 0;
  7.     FILE *fp = fopen (text, "rb");
  8.     while ((c = fgetc (fp)) != EOF) {
  9.         if (c == string[offset]) {
  10.             if (offset == 0)
  11.                 start = i;
  12.             if (offset == (strlen (string) - 1)) {
  13.                 fclose (fp);
  14.                 return start;
  15.             }
  16.             offset++;
  17.         } else {
  18.             offset = 0;
  19.             start = 0;
  20.             i++;
  21.        }
  22.     }
  23.     fclose (fp);
  24.     return -1;
  25. }
  26.  
  27. int main (int argc, char *argv[]) {
  28.     if (argc != 3) {
  29.         fprintf (stderr, "USAGE: ./bitap [filename] [string]\n");
  30.         return 1;
  31.     } else {
  32.         unsigned long a = stateMachineSearch (argv[1] , argv[2]);
  33.         printf ("pos: %zd\n", a);
  34.         return 0;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement