/*** string matching algorithm ***/#include <cstring> #include <iostream>using namespace std; #define OK 1#define ERROR 0#define overflow-2typedef int Status; #define Maxstrlen 255 //user can define the longest string of long typedef char sstring[maxstrlen+1];//within 255 Unit No. 0 Stores the length of the string status Strassign (sstring T, char *chars) {//generates a string whose value equals chars tint i;if (strlen (chars) > Maxstrlen) return Erro R;else {T[0] = strlen (chars); for (i = 1; I <= t[0]; i++) T[i] = * (chars + i-1); return OK;} Fill in the 4.1 bf algorithm int Index (sstring s, sstring t, int pos) {//return mode T in the main string S in the first POS character after the first occurrence of the position. If not present, the return value is 0//where T non-null, 1≤pos≤strlength (S) int i=pos;int j=1;while (i<=s[0]&&j<=t[0]) {if (S[i]==t[j]) {i++ ; j + +;} Else{i=i-j+2;j=1;}} if (j>t[0]) return i-t[0];else return 0;} Indexint Main () {sstring S; Strassign (S, "Bbaaabbaba"); Sstring T; Strassign (T, "ABB");cout<< "main string and substring in section" <<index (s,t,1) << "Word prompt first match \ n"; return 0;}
Pattern matching-BF algorithm