-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpattern_matching.c
77 lines (70 loc) · 1.17 KB
/
pattern_matching.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
76
77
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void read();
void display();
void pattern();
char mainstr[100],patstr[100],repstr[100],tempstr[100];
void main()
{
read();
printf("String before pattern match:");
puts(mainstr);
display();
pattern();
printf("String After pattern match:");
puts(mainstr);
}
void read()
{
printf("Enter the main string ,pattern string and replacement string respectively:\n");
gets(mainstr);
gets(patstr);
gets(repstr);
}
void display()
{
printf("The entered main string ,pattern string and replacement string are:\n");
puts(mainstr);
puts(patstr);
puts(repstr);
}
void pattern()
{
int mi=0,pi=0,ti=0,ci=0,ri=0,flag=0;
while(mainstr[mi]!='\0')
{
while((mainstr[ci]==patstr[pi])&&((mainstr[ci]!='\0')||(patstr[pi]!='\0')))
{
ci++;
pi++;
}
if(patstr[pi]=='\0')
{
for(ri=0;repstr[ri]!='\0';ri++)
{
tempstr[ti]=repstr[ri];
ti++;
}
mi=ci;
pi=0;
flag=1;
}
tempstr[ti]=mainstr[mi];
mi++;
ci=mi;
pi=0;
ti++;
}
if(flag==0)
{
printf("Pattern does not found.\n");
}
else
{
tempstr[ti]='\0';
for(ti=0;tempstr[ti]!=0;ti++)
mainstr[ti]=tempstr[ti];
mainstr[ti]='\0';
}
}