The following source codes are used in this chapter. In C, we use the term string to refer to a variable-length array of characters, defined by a starting point and by a string-termination character marking the end. C++ inherits this data structure from C, also includes strings as a higher-level abstraction in the standard library.
Naive search
// Data Structure & Algorithm C++, Otgonbayar Tovuudorj
// Chapter 15: Strings
// Naive search
#include <string.h>
#include <iostream>
using namespace std;
// search first occurrence only and return its address
char *naivesearch1(char *pat, char *text) {
register char *bp, *sp;
if (*pat)
while (*text) {
bp = text; sp = pat;
do {
if (!*sp) return text;
else if(!*bp) return 0;
} while (*bp++ == *sp++);
text++;
}
return 0;
}
// search first occurrence only and return it location
int naivesearch(char *pat, char *text) {
int i, j, m = strlen(pat), n = strlen(text);
for(i=0, j=0; j<m && i<n; i++, j++)
if(text[i] != pat[j]) { i -= j; j = -1; }
if ((j==m) && m)
return i-m+1;
else
return 0;
}
// search all occurrences and prints their locations
void naivesearchall(char *pat, char *text) {
int i, j, m = strlen(pat), n = strlen(text);
for(i=0, j=0; j<=m && i<=n; i++, j++)
if((text[i] != pat[j]) || ( !text[i] && !pat[j])) {
if ((j == m) && m)
cout << " found at location: "<< i-m+1 << endl;
i -= j;
j = -1;
}
}
int main() {
char s[] = "This is most simple searching algorithm";
char s1[40];
cout << "Text: " << s << endl;
cout << "string to be searched: "; gets(s1);
naivesearchall(s1, s);
cout << naivesearch(s1, s) << endl;
cout << naivesearch1(s1, s) << endl;
}
KMP search
// Data Structure & Algorithm C++, Otgonbayar Tovuudorj
// Chapter 15: Strings
// KMP search
#include <iostream>
#include <string.h>
using namespace std;
void initnext(char *pat, int next[], int m) {
int i, j;
next[0] = -1;
for(i=0, j= -1; i<m; i++, j++, next[i] = j)
while((j>=0) && (pat[i] != pat[j]))
j = next[j];
}
int kmpsearch(char *pat, char *text) {
int i, j, m = strlen(pat), n = strlen(text);
int *next = new int[m];
if(*pat) {
initnext(pat, next, m);
for(i=0, j=0; j<m && i<n; i++, j++)
while((j>=0) && (text[i] != pat[j]))
j = next[j];
if(j == m)
return i-m+1;
else
return 0;
}
return 0;
}
int main(void)
{
char text[] = "111111100111010010100010100111000111";
char p[40];
int res;
cout << "Text: " << text << endl;
cout << "Input string: "; gets(p);
res = kmpsearch(p, text);
cout << res << endl;
}
Boyer-Moore search
// Data Structure & Algorithm C++, Otgonbayar Tovuudorj
// Chapter 15: Strings
// Boyer-Moore search
#include <iostream>
#include <string.h>
using namespace std;
#define max(A, B) (A > B) ? A : B;
int pos[256];
void initpos(char *pat) {
int j, M = strlen(pat);
for (j = 0; j < 256; j++) pos[j] = M;
for (j = 0; j < M; j++) pos[pat[j]] = M-j-1;
}
int bmsearch(char *pat, char *text) {
int i, j;
int M = strlen(pat), N = strlen(text);
initpos(pat);
for (i = M-1, j = M-1; j >= 0; i--, j--)
while (text[i] != pat[j]) {
i += max(M-j, pos[text[i]]);
if (i >= N) return 0; // not found
j = M-1;
}
return i+2; // at location: i+2
}
int main() {
char text[] = "this is a searching examples for boyer moore algorithm&";
char pat[50];
int loc;
cout << text << " - "<< strlen(text) << endl;
cout << "Input string: "; gets(pat);
if (loc = bmsearch(pat, text))
cout << "Found at location: " << loc <<endl;
else
cout << "Not found" << endl;
}
