Created
June 15, 2014 07:47
-
-
Save sunny1304/51e677ce9ed1e6ca579b to your computer and use it in GitHub Desktop.
simple string match algorithm
This file contains hidden or 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 <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int string_match(char* str, char* pattern) | |
{ | |
size_t str_len = strlen(str); | |
size_t pattern_len = strlen(pattern); | |
size_t len_diff = str_len - pattern_len; | |
int i,matched=0; | |
for (i=0; i<= len_diff;) | |
{ | |
int j=0; | |
if (str[i] == pattern[j]) | |
{ | |
for(j=1; j< pattern_len; j++) | |
{ | |
if (str[i+j] != pattern[j]) | |
{ | |
break; | |
} | |
else | |
{ | |
if (j+1 == pattern_len) | |
{ | |
matched++; | |
i+= j; | |
} | |
} | |
} | |
} | |
i++; | |
} | |
return matched; | |
} | |
int main() | |
{ | |
int x = string_match("aswqsdasd", "dasd"); | |
printf("%d\n", x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment