-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.c
More file actions
56 lines (56 loc) · 1.27 KB
/
Copy pathLab2.c
File metadata and controls
56 lines (56 loc) · 1.27 KB
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
#include <stdio.h>
char ans[100], str[100], pat[100], rep[100];
int flag, i, j, k, l, p;
void matchpattern()
{
while (str[i] != '\0')
{
// Checking for Match
if (str[j] == pat[p])
{
p++;
j++;
if (pat[p] == '\0') /*to check pattern reach end or not */
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, l++) // copy replace string in ANS string
{
ans[l] = rep[k];
}
p = 0; // if more occurence of pattern
i = j; /*next character after pattern is matched */
}
}
else // mismatch
{
ans[l] = str[i];
l++;
i++;
j = i;
p = 0;
}
}
ans[l] = '\0';
}
void readstr()
{
printf("Enter the main string: \n");
scanf("%[^\n]", str);
printf("Enter a pattern string: \n");
scanf("%*c%[^\n]", pat); /* %*c is to skip enter keypress */
printf("Enter a replace string: \n");
scanf("%*c%[^\n]", rep);
}
main()
{
readstr();
matchpattern();
if (flag == 0)
{
printf("Pattern not found!!!\n");
}
else
{
printf("The RESULTANT string is:\n%s\n", ans);
}
}