Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple string match algorithm
#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