利用c语言写一个串的简单模式匹配布鲁特福斯算法
时间: 2023-07-31 19:10:12 浏览: 50
好的,下面是一个简单的布鲁特福斯模式匹配算法的C语言实现:
```c
#include <stdio.h>
#include <string.h>
void bruteForce(char *text, char *pattern) {
int n = strlen(text);
int m = strlen(pattern);
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (text[i+j] != pattern[j])
break;
}
if (j == m)
printf("Found pattern at index %d\n", i);
}
}
int main() {
char text[] = "hello world";
char pattern[] = "world";
bruteForce(text, pattern);
return 0;
}
```
这个算法的基本思想是:对于文本串text和模式串pattern,从文本串的第一个位置开始,逐个比较文本串中与模式串匹配的子串。如果匹配成功,输出匹配的位置。
在代码中,我们首先计算出文本串text和模式串pattern的长度,然后从文本串的第一个位置开始,逐个比较文本串中与模式串匹配的子串。如果匹配成功,输出匹配的位置。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)