In this program, we are going to share a C program for Naive Pattern Searching algorithm. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program for Naive Pattern Searching algorithm with the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include <string.h> void search(char* pat, char* txt) { int M = strlen(pat); int N = strlen(txt); for (int i = 0; i <= N - M; i++) { int j; for (j = 0; j < M; j++) if (txt[i + j] != pat[j]) break; if (j == M) printf("Pattern found at index %d \n", i); } } int main() { char txt[] = "AABAACAADAABAAABAA"; char pat[] = "AABA"; search(pat, txt); return 0; } |
Pattern found at index 0
Pattern found at index 9
Pattern found at index 13
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.