Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
暴力
1 int strStr(char* haystack, char* needle) { 2 int len_hay; 3 int len_need; 4 int i,j; 5 len_hay = strlen(haystack); 6 len_need = strlen(needle); 7 for(i = 0; i <= len_hay - len_need; i++) 8 { 9 for(j = 0; j < len_need; j++) 10 { 11 if(needle[j] != haystack[i+j]) 12 break; 13 } 14 if(j == len_need) 15 return i; 16 } 17 return -1; 18 19 }
KMP 以後寫。。