数据结构之朴素字符串匹配

#include < stdio.h >
#include < string.h >
int native_string_matcher(char *str_source,char *str_pattern)
{
int len_source = strlen(str_source);
int len_pattern = strlen(str_pattern);
if(str_source == NULL && str_pattern == NULL && len_source < len_pattern)
return 0;
int i,j,tmp;
for(i = 0;i <= len_source-len_pattern; i++)
{
tmp = i;
j = 0;
while(j
{
if(str_source[i++] != str_pattern[j++])
break;
if(j == len_pattern)
return 1;
}
i = tmp;
}
return 0;
}


int main()
{
char *str_source = "Hello,my name is Gavin Ge";
char *str_pattern = "name";
//char *str_pattern = "namee";
if (native_string_matcher(str_source,str_pattern))
printf("%s\n", "Yes, Match!");
else
printf("%s\n", "No, Not Match!");
return 0;
}
posted @ 2014-07-13 17:14  dreamsyeah  阅读(218)  评论(0编辑  收藏  举报