-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_Naive String.c
More file actions
37 lines (37 loc) · 936 Bytes
/
10_Naive String.c
File metadata and controls
37 lines (37 loc) · 936 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
//10_Naive String.c
#include <stdio.h>
#include <string.h>
int match(char st[], char pat[]);
int main()
{
char st[100], pat[100];
int status;
printf("*** Naive String Matching Algorithm ***\n");
printf("Enter the string: ");
fgets(st, 100, stdin);
st[strcspn(st, "\n")] = '\0'; // remove trailing newline
printf("Enter the pattern to match: ");
fgets(pat, 100, stdin);
pat[strcspn(pat, "\n")] = '\0'; // remove trailing newline
status = match(st, pat);
if (status == -1)
printf("\nNo match found.\n");
else
printf("Match found at position %d.\n", status);
return 0;
}
int match(char st[], char pat[])
{
int n, m, i, j;
n = strlen(st);
m = strlen(pat);
for (i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
if (st[i + j] != pat[j])
break;
}
if (j == m)
return i;
}
return -1;
}