-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_update.c
46 lines (44 loc) · 873 Bytes
/
string_update.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
#include <stdio.h>
char new_pattern[100000];
int stlen(char *str)
{
int i=0;
while(str[i])i++;
return i;
}
char * update(char *text,char *pattern_to_replace,char * pattern_to_insert)
{
int i,j,sa=stlen(text),sb=stlen(pattern_to_replace),k,l,sc=stlen(pattern_to_insert);
for(i=0;i<100000;i++)new_pattern[i]=0;
for(i=0,k=0;i+sb<sa;i++)
{
int flag=1;
for(j=0;j<sb;j++)
{
if(text[i+j]!=pattern_to_replace[j])
{
flag=0;
break;
}
}
if(flag)
{
for(l=0;l<sc;l++,k++)
{
new_pattern[k]=pattern_to_insert[l];
}
i+=sb-1;
}
else
{
new_pattern[k++]=text[i];
}
}
for(;i<sa;i++)new_pattern[k++]=text[i];
return new_pattern;
}
int main()
{
char text[]="hello my world myworld hello world my world",pattern_to_replace[]="my",pattern_to_insert[]="our";
printf("%s\n", update(text,pattern_to_replace,pattern_to_insert));
}