Advertisement
cd62131

strstr

Mar 18th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main(void) {
  4.   FILE *in = fopen("a.txt", "r");
  5.   const char *s = "ab";
  6.   char buf[BUFSIZ];
  7.   int c = 0;
  8.   while (fgets(buf, BUFSIZ - 1, in)) {
  9.     char *p = buf;
  10.     while (1) {
  11.       char *p2 = strstr(p, s);
  12.       if (p2 != NULL) c++;
  13.       else break;
  14.       p = p2 + strlen(s);
  15.     }
  16.   }
  17.   fclose(in);
  18.   printf("%d\n", c);
  19.   return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement