重复搜索相同文本的C ++程序(例如,通过构建数据结构来搜索圣经)
这是一个C++程序,用于重复搜索相同的文本。
演算法
Begin
Take the original string and pattern to be searched as input.
org_len = store the length of original string
pat_len = store the length of pattern
for i = 0 to (org_len - pat_len)
for j = 0 to pat_len - 1
if (org[i + j] != patt[j])
if (j == pat_len)
Increase m.
Print the position at which the pattern is found
if (m == 0)
Print no match found
else
Print the total number of instances found.
return 0
End示例
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char org[150], patt[150];
int i, j, m = 0, org_len, pat_len;
cout << "\nEnter Original String:";
cin >> org;
cout << "输入要搜索的模式:";
cin >> patt;
org_len = strlen(org); //store the length of original string
pat_len = strlen(patt); //store the length of pattern
for (i = 0; i <= (org_len - pat_len); i++) {
for (j = 0; j < pat_len; j++) {
if (org[i + j] != patt[j])
break;
}
if (j == pat_len) {
m++;
cout << "\nPattern Found at Position: " << i;
}
} if (m == 0)
cout << "\nNo Match Found.";
else
cout << "\nTotal Number of Instances Found = " << m;
return 0;
}输出结果
Enter Original String:thisisnhooo.thisisac++program 输入要搜索的模式:is Pattern Found at Position: 2 Pattern Found at Position: 4 Pattern Found at Position: 23 Pattern Found at Position: 25 Total Number of Instances Found = 4
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短