-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaivestringMatching.c
More file actions
37 lines (33 loc) · 860 Bytes
/
NaivestringMatching.c
File metadata and controls
37 lines (33 loc) · 860 Bytes
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
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <string.h>
int compare(char text[], char pattern[]) {
int n = strlen(text);
int m = strlen(pattern);
int flag = -1;
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (text[i + j] != pattern[j])
break;
}
if(j==m){
printf("Pattern found at index %d\n",i);
flag = 1;
}
}
return flag;
}
int main(int argc, char **argv) {
char s[100], p[100];
int status;
printf("*** Naive String Matching Algorithm ***\n");
printf("Enter the String.\n");
scanf("%s",s);
printf("Enter the pattern to match.\n");
scanf("%s",p);
int ans = compare(s, p);
if(ans == -1){
printf("String match not found \n");
}
return 0;
}