1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| #include <stdio.h>
#include <stdlib.h>
#include "string.h"
#define N 10000
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("%-8s%s\n%s\n%s\n",
"Usage: ", "need two arguements",
"1.string -- the word you want to find",
"2.file -- the file you find the word in");
exit(1);
}
int i, j, t, count = 0;
char a[N], *p = argv[1];
FILE *fin = fopen(argv[2], "r");
if (!fin) {
printf("%s: No such file\n", argv[2]);
exit(1);
}
for (i = 0; i < N-1, (t = getc(fin)) != EOF; a[i] = t, i++);
a[i] = '\0';
for (i = 0; a[i] != '\0'; i++) {
for (j = 0; p[j] != '\0'; j++) {
if (a[i+j] != p[j]) break;
}
if (p[j] == '\0') {
count++;
printf("%d ", i);
}
}
printf("\nuse strncmp: ");
for (i = 0; i < exile_strlen(a); i++)
if (exile_strncmp(&a[i], p, exile_strlen(p)) == 0)
printf("%d " ,i);
printf("\nfind %d \"%s\" in file %s\n", count, argv[1], argv[2]);
}
|