-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkunth mores Parth.c
75 lines (71 loc) · 1.31 KB
/
kunth mores Parth.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
68
69
70
71
72
73
74
75
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int kmp(char *str, char *word,int *ptr)
{
int i=0,j=0;
while((i+j)<strlen(str))
{
if(word[j]==str[i+j])
{
if(j==strlen(word)-1)
{
printf("%s located at index %d\n",word,i+1);
return;
}
j=j+1;
}
else
{
i=i+j-ptr[j];
if(ptr[j]>-1)
{
j=ptr[j];
}
else
{
j=0;
}
}
}
}
void findOverlap(char *word,int *ptr)
{
int i=2,j=0,len=strlen(word);
ptr[0]=-1;
ptr[1]=0;
while(i<len)
{
if(word[i-1]==word[j])
{
j=j+1;
ptr[i]=j;
i=i+1;
}
else if(j>0)
{
j=ptr[j];
}
else
{
ptr[i]=0;
i=i+1;
}
}
return;
}
int main()
{
char word[256],str[1024];
int *ptr,i;
printf("enter Text: ");
fgets(str,1024,stdin);
str[strlen(str)-1]='\0';
printf("enter pattern: ");
fgets(word,256,stdin);
word[strlen(word)-1]='\0';
ptr=(int *)calloc(1,sizeof(int)*(strlen(word)));
findOverlap(word,ptr);
kmp(str,word,ptr);
return 0;
}