int kmpstrstr(const char *text, const char *pattern) { const int Pattern_Max = 256; static unsigned char next[Pattern_Max]; int i, j; int len = strlen(pattern); if (len <= 0 || len > Pattern_Max) { return -1; } i = 1; j = 0; next[1] = 0; while (pattern[i]) { if (pattern[i] == pattern[j]) { next[++i] = ++j; } else if (j == 0) { next[++i] = j; } else { j = next[j]; } } i = j = 0; while (text[i] && pattern[j]) { if (text[i] == pattern[j]) { i++; j++; } else if (j == 0) { i++; } else { j = next[j]; } } return (pattern[j] ? -1 : (i-j)); }