Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Created June 15, 2014 07:47
Show Gist options
  • Save sunny1304/51e677ce9ed1e6ca579b to your computer and use it in GitHub Desktop.
Save sunny1304/51e677ce9ed1e6ca579b to your computer and use it in GitHub Desktop.
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