forked from Adoby7/CLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BF.c
39 lines (36 loc) · 563 Bytes
/
BF.c
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
#include <stdio.h>
#include <string.h>
/*
* if find,return first offset
* else return -1
*/
int BF_match(char *text,char *pattern)
{
size_t n = strlen(text);
size_t m = strlen(pattern);
int i;
int end = n-m;
for(i = 0; i <= end; i++)
{
int j;
char *p_text = text + i;
for(j = 0;j < m;j++)
{
if(*(p_text+j) == *(pattern+j))
continue;
else
break;
}
if(j == m)
return i;
}
return -1;
}
int main()
{
char *s1 = "abcabcabcd";
char *s2 = "abcd";
int offset = BF_match(s1,s2);
printf("offset is %d\n",offset);
return 0;
}