Skip to content

Instantly share code, notes, and snippets.

@obstschale
Created July 6, 2012 12:57
Show Gist options
  • Save obstschale/3060031 to your computer and use it in GitHub Desktop.
Save obstschale/3060031 to your computer and use it in GitHub Desktop.
Brute-Force Pattern Matching
int bruteforcematch(char S[ ], char P[ ]) // S is source string, P is pattern
{
int n, m, i, j;
n = strlen(S);
m = strlen(P);
for (i = 0; i <= n – m; i++) // for each pattern comparison
{
j = 0;
while(j < m && S[i+j] == P[j]) // each character comparison
{ j++; } // end while
if (j == m)
{ return i; }
else
{ return -1; } // not matched
} // end for
}
@obstschale
Copy link
Author

Brute-Force Pattern Matching
only suitable for small strings with very few repeated sequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment