-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_040.cpp
65 lines (58 loc) · 1.27 KB
/
Day_040.cpp
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
/**
*Problem Statement: a Program to Replace substring in a string.
*Author: Kunal Kathpal (https://github.com/kunal-2002)
*/
#include <bits/stdc++.h>
using namespace std;
void replace_substr(char str[], char sub[], char nstr[]){
int strLen, sublen, nstrlen;
int i = 0, j, k;
int flag = 0, start, end;
strLen = strlen(str);
sublen = strlen(sub);
nstrlen = strlen(nstr);
for(i =0; i<strLen; i++){
flag = 0;
start = i;
for(j = 0; str[i] == sub[j];j++,i++){
if(j == sublen-1)
flag = 1;
}
end = i;
if(flag == 0)
i -= j;
else{
for(j = start; j<end; j++){
for(k = start; k<strLen; k++)
str[k] = str[k+1];
strLen--;
i--;
}
for(j=start;j<start+nstrlen;j++)
{
for(k=strLen;k>=j;k--)
str[k+1]=str[k];
str[j]=nstr[j-start];
strLen++;
i++;
}
}
}
}
int main(){
cout<<"Enter number of test cases:\t";
int T;
cin>>T;
while(T--){
char str[20], substr[20], newstr[20];
cout<<"Enter a string:\n";
cin>>str;
cout<<"Enter the substring to be removed:\n";
cin>>substr;
cout<<"Enter the new substring:\n";
cin>>newstr;
replace_substr(str,substr, newstr);
cout<<"The new string:\n"<<str<<endl;
}
return 0;
}