Created
June 17, 2013 13:35
-
-
Save mryoshio/5796902 to your computer and use it in GitHub Desktop.
string simple search algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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