Created
July 25, 2017 13:40
-
-
Save porimol/12844863133dcb55a9a3cf1c9fb04b87 to your computer and use it in GitHub Desktop.
C program for Naive Pattern Searching algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
void search(char *pat, char *txt) | |
{ | |
int i = 0; | |
int M = strlen(pat); | |
int N = strlen(txt); | |
// A loop to slide pat[] one by one | |
for (i = 0; i <= N - M; i++) | |
{ | |
int j; | |
// For current index i, check for pattern match | |
for (j = 0; j < M; j++) | |
if (txt[i+j] != pat[j]) | |
break; | |
// if j equal m then print match found | |
if (j == M) | |
printf("\nPattern found at index %d n\n\n", i); | |
} | |
} | |
int main() | |
{ | |
char txt[100]; | |
char pat[50]; | |
printf("Please enter the text/string: "); | |
gets(txt); | |
printf("\nPlease enter the pattern: "); | |
gets(pat); | |
search(pat, txt); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment