-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeceficPartDelete.c++
More file actions
114 lines (89 loc) · 3.03 KB
/
speceficPartDelete.c++
File metadata and controls
114 lines (89 loc) · 3.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//I wrote a code to remove a substring from a string using the standard <algorithm> library and also without the library.
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
class MyString{
private:
string str;
public:
MyString(const string& ini="" ):str(ini){} //This is the constructor of the class.
void print(){
cout<<endl;
for(auto i :str ){
cout<<i;
}
cout<<endl;
}
void in(){ //We take in the input of the string.
cout<<"\nPlease enter the text you want to transform : ";
cin>>str;
}
string rem(){
clock_t start , end ;
//We are using the erase() method of standard c++ algorithms.
cout<<"\nPlease enter the part of the text you want to remove : ";
string inp;
cin>>inp;
start = clock();
str.erase(str.find(inp) , inp.length());
end = clock();
cout<<"\nThe total time taken to transform the string is : "<<end - start << " ms.\n\n";
return str;
}
string remBrute(){
//We are writing the implementation of the above algo using the simple methods
clock_t start , end;
cout<<"Please enter the substring you want to remove : ";
string sub;
cin>>sub;
start = clock();
int index,c1 ,c2,co;
int su = sub.length();
int sr = str.length();
c1=0;
for(int i = 0 ;i<su ; i++ ){
c2=0;
for(int j = 0; j<sr ; j++){
if(str[j]==sub[i] && c2==0){
co=j;
c2++;
}
}
int co1 =co ;
for(int i = 0 ; i<su ; i++){
if(str[co1]==sub[i]){
c1++;
co1++;
}
}
if(c1 == sub.length()){
for(int i = co ; i<su+co ; i++){
str[i] = '_';
}
}
}
end = clock();
cout<<"\nThe time taken to run the program is : "<<end - start <<" ms.";
return str;
}
};
int main(){
MyString str;
str.in();
int sw;
cout<<"\nPlease enter 1 for standard and 2 for brute method : ";
cin>>sw;
if(sw==1){
str.rem();
}
else{
str.remBrute();
}
cout<<"\nThe string after being transformed is :";
str.print();
return 0;
}
/*After comparing both the programs we get that the both work the same way and thus we can roughly imagine the
mechanism of rem() using remBrute() .*/