int main() { int count = 0; char inputStringArray[] = "ABABDABACDABABDABACDABABCABAB"; char patArray[] = "ABABCABAB"; int pattLen = strlen(patArray); int textLen = strlen(inputStringArray); int matchPosition = 0; // index for inputStringArray[] // create lps[] that will hold the longest prefix suffix // values for pattern int *lpsArray = (int *)calloc(pattLen, sizeof(int)); // Preprocess the pattern (calculate lps[] array) computeLPSArray(patArray, pattLen, lpsArray); for (count = 0; count < pattLen; count++) printf("%d ", lpsArray[count]); matchPosition = kmp_matcher(inputStringArray, textLen, patArray, pattLen, lpsArray); if (matchPosition != -1) printf("\nMatch found in the original input string at = %d\n", matchPosition); else printf("\nPattern Not Found The Original Input String\n"); free(lpsArray); // to avoid memory leak }
int main() { char *target = "abababaababacb"; char *pattern = "ababacb"; char *p = NULL; char *test = "ababababca"; int *ta = NULL; int i = 0; ta = (int *)malloc(strlen(test) *sizeof(int)); compute_prefix_function(test, ta); printf("Target : %s\nPattern : %s\n", target, pattern); p = kmp_matcher(target, pattern); if (p) printf("Pattern found at position : %d\n", p - target); else printf("Pattern not found!\n"); for(i = 0; i < 10; i++) { printf("%d\t", *(ta + i)); } printf("\n"); return 0; }
main() { char t[] ="abadefde", p[]="bad"; //naive_string_matcher(t,p); int i = kmp_matcher(t, strlen(t), p, strlen(p)); printf("%d\n", i); return 0; }
int main(){ char a[9] = { 'a', 'b', 'a', 'c', 'b', 'c','d','h','i' }; char b[4] = { 'c', 'b', 'c','d' }; //int s = naive_string_match(a, b, 6, 3); int x = kmp_matcher(a, b, 9, 4); printf("\n %d", x); //printf("%d\n", s); system("pause"); return 0; }
int main(int argc, char *argv[]) { int n; scanf("%d", &n); while(n--){ scanf("%s", ptn); scanf("%s", text); compute_prefix(ptn, strlen(ptn)); printf("%d\n", kmp_matcher(text, strlen(text), ptn, strlen(ptn))); } return 0; }
int main(int argc, const char * argv[]) { char T[1000],P[100]; printf("\nPlease enter the text: \n"); while(gets(T)){ printf("\nPlease enter the pattern: \n"); gets(P); int pos = kmp_matcher(T,P); if(pos != -1){ printf("\nString Occur at position:%d \n",pos+1); }else{ printf("\nNo match found.\n"); } printf("\nPlease enter the text: \n"); } return 0; }
int main(int argc, char *argv[]) { int N; scanf("%d", &N); __clear_input_buf(); int *result = (int *)malloc(sizeof(int) * N); char *source = NULL; char *patt = NULL; size_t n; int i; for(i=0;i<N;i++) { __getline(&patt, &n, stdin); __getline(&source, &n, stdin); result[i] = kmp_matcher(source, strlen(source), patt, strlen(patt)); } for(i=0;i<N;i++) printf("%d\n", result[i]); free(source); free(patt); free(result); return 0; }
char * replace(const char *str, const char *findstr, const char *replacestr) { char *oldstr = str; char *newstr = NULL; char *plocation = NULL; int newstrlen = 0; int findstrlen = 0; int replacestrlen = 0; int diff = 0; int location = 0; int cplocation = 0; newstrlen = strlen(oldstr); findstrlen = strlen(findstr); replacestrlen = strlen(replacestr); diff = replacestrlen - findstrlen; if(diff <= 0) { diff = 0; } if((newstr = (char *)malloc(sizeof(char) * newstrlen)) == NULL) { printf("malloc failed.\n"); exit(0); } memset(newstr, '\0', sizeof(newstr)); do { plocation = kmp_matcher(oldstr, findstr); if (plocation) { newstrlen += diff; location = plocation - oldstr; if ((newstr = (char *)realloc(newstr, sizeof(char) * newstrlen)) == NULL) { printf("realloc failed.\n"); exit(0); } strncpy(newstr + cplocation, oldstr, location); cplocation += location; oldstr += location; oldstr += findstrlen; strncpy(newstr + cplocation, replacestr, replacestrlen); cplocation += replacestrlen; } else { strncpy(newstr + cplocation, oldstr, strlen(oldstr)); strncpy(newstr + cplocation + strlen(oldstr), "\0", 1); } } while (plocation != NULL); return newstr; }