* main.c
* 简单的字符串匹配
* Created on: Nov 6, 2010
* Author: jenson
*/
#include <stdio.h>
#include <stdlib.h>
#define N 10000
/**
* src:源字符串,str:所要匹配的字符串
*/
int search(const char*src, const char *str);
int main() {
int pos = search("bbabcaaabbb","abc");
printf("pos=%d\n",pos);
}
int search(const char*src, const char *str) {
int src_len = strlen(src);
int str_len = strlen(str);
if (src_len < str_len) {
return -1;
}
int i,j, pos = -1;
for(i = 0;src[i]!='\0';i++){
for(j=0;str[j]!='\0';j++){
if(src[i+j]!=str[j]){
break;
}
}
if(str[j] == 0){
return i;
}
}
}
这个算法的效率很低。时间为N^2