Advertisement
cd62131

search string

Jan 27th, 2019
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main(int argc, const char **argv) {
  5.   if (argc != 3) {
  6.     exit(1);
  7.   }
  8.   FILE *in = fopen(argv[1], "r");
  9.   if (!in) {
  10.     exit(1);
  11.   }
  12.   long count = 0;
  13.   for (char buf[BUFSIZ]; fgets(buf, BUFSIZ, in);) {
  14.     for (char *p = buf, *q; p < buf + BUFSIZ && (q = strstr(p, argv[2]));
  15.          ++count, p = q + 1) {
  16.       ;
  17.     }
  18.   }
  19.   fclose(in);
  20.   printf("%s is %ld.\n", argv[2], count);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement