forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knutt_Morris_Prat.c
67 lines (61 loc) · 1.77 KB
/
Knutt_Morris_Prat.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char string[100], matchcase[20], c;
int i = 0, j = 0, index;
/*Scanning string*/
printf("Enter string: ");
do
{
fflush(stdin);
c = getchar();
string[i++] = tolower(c);
} while (c != '\n');
string[i - 1] = '\0';
/*Scanning pattern to search*/
printf("Enter substring: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
matchcase[i++] = tolower(c);
} while (c != '\n');
matchcase[i - 1] = '\0';
for (i = 0; i < strlen(string) - strlen(matchcase) + 1; i++) /*Search in the range of len(string) - Len(pattern) + 1*/
{
index = i;
if (string[i] == matchcase[j]) /*Check if charatcers match*/
{
/*Continue checking until the characters match or the end of pattern string is reached*/
do
{
i++;
j++;
} while(j != strlen(matchcase) && string[i] == matchcase[j]);
if (j == strlen(matchcase)) /*if j is equal to pattern length , pattern is found.*/
{
printf("Match found from position %d to %d.\n", index + 1, i);
return 0;
}
else /*if j is not eual to pattern length, move to next character and continue the same steps.*/
{
i = index + 1;
j = 0;
}
}
}
printf("No substring match found in the string.\n");
return 0;
}
/*
Time Complexity: O(n + m) - n is the length of string and m is the length of substring
Space Complexity: O(m) - m is the length of substring
Sample Input:
Enter string: ABCABAABCABAC
Enter subsring: CAB
Output:
Match found from position 3 to 5
*/