Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created June 17, 2013 13:35
Show Gist options
  • Save mryoshio/5796902 to your computer and use it in GitHub Desktop.
Save mryoshio/5796902 to your computer and use it in GitHub Desktop.
string simple search algorithm
#include <iostream>
#include <string>
using namespace std;
unsigned char original_text[] = "Team Swift";
unsigned char original_pattern[] = "if";
unsigned char *simple_search(unsigned char* text, unsigned char* pattern) {
int i;
while ((*text) != '\0') {
cout << endl;
cout << original_text << endl;
for (i = 0; i < text - original_text; i++) {
cout << " ";
}
cout << pattern;
for (i = 0; pattern[i] != '\0'; i++) {
if (pattern[i] != text[i])
break;
}
if (pattern[i] == '\0')
return text;
text++;
}
return NULL;
}
int main() {
unsigned char *result;
result = simple_search(original_text, original_pattern);
if (result == NULL) {
cout << endl << "NOT Found" << endl;
} else {
cout << " -> Found" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment