C语言代码实现BF算法
时间: 2023-11-25 07:50:22 浏览: 217
BFBM算法C语言实现
以下是C语言实现BF算法的代码示例:
```c
#include <stdio.h>
#include <string.h>
int BF(char *s, char *p) {
int i = 0, j = 0;
int s_len = strlen(s);
int p_len = strlen(p);
while (i < s_len && j < p_len) {
if (s[i] == p[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == p_len) {
return i - j;
} else {
return -1;
}
}
int main() {
char s[] = "hello world";
char p[] = "world";
int pos = BF(s, p);
if (pos == -1) {
printf("Not found\n");
} else {
printf("Found at position %d\n", pos);
}
return 0;
}
```
阅读全文