-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha02.cpp
More file actions
349 lines (344 loc) · 5.88 KB
/
a02.cpp
File metadata and controls
349 lines (344 loc) · 5.88 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//============================================================================
// Assignment 2
// Roll No. : 21164
// Name : Sooraj VS
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<bits/stdc++.h>
using namespace std;
class str
{
char ch[200];
public:
str(){ ch[0]='\0'; }
void read();
void display();
int substr(str);
int subStr(str);
int replace(str,str);
void copy(str);
void menu();
int len();
void concat(str);
void reverse();
int compare(str);
int frequency(char);
int palindrome();
str chardelete(char);
str modify(int,int);
};
str str::modify(int start,int span)
{
str s;
int i,j=0;
for(i=0;i<len();i++)
{
if(i==start)
i=i+span;
else
{
s.ch[j]=ch[i];
j++;
}
}
s.ch[j]='\0';
return s;
}
str str::chardelete(char c)
{
str s;
int i,j=0;
for(i=0;i<len();i++)
{
if(ch[i]!=c)
{
s.ch[j]=ch[i];
j++;
}
}
if(i==j)
cout<<"\t---->Character: "<<c<<" Not Found"<<endl;
s.ch[j]='\0';
return s;
}
int str::palindrome()
{
int i,f=1,l=len();
for(i=0;i<len();i++)
if(ch[i]!=ch[l-i-1])
f=0;
if(f)
return 1;
else
return 0;
}
int str::frequency(char c)
{
int i,count=0;
for(i=0;i<len();i++)
{
if(ch[i]==c)
count++;
}
return count;
}
int str::compare(str s1)
{
int f=1,i,l,l1;
l=len();
l1=s1.len();
for(i=0;i<l;i++)
if(ch[i]!=s1.ch[i])
f=0;
if(l1==l&&f)
return 1;
else
return 0;
}
void str::reverse()
{
int i,l=len();
char temp;
for(i=0;i<l/2;i++)
{
temp=ch[i];
ch[i]=ch[l-i-1];
ch[l-i-1]=temp;
}
ch[l]='\0';
}
void str::menu()
{
cout<<endl<<"------------------STRING FUNCTIONS--------------------"<<endl;
cout<<"\tPress 1 : Enter String"<<endl;
cout<<"\tPress 2 : Display"<<endl;
cout<<"\tPress 3 : Find Length"<<endl;
cout<<"\tPress 4 : Copy"<<endl;
cout<<"\tPress 5 : concatenate"<<endl;
cout<<"\tPress 6 : find substring"<<endl;
cout<<"\tPress 7 : replace a substring"<<endl;
cout<<"\tPress 8 : Reverse the string"<<endl;
cout<<"\tPress 9 : compare with another string"<<endl;
cout<<"\tPress 10 : check frequency of any Character"<<endl;
cout<<"\tPress 11 : check Palindrome"<<endl;
cout<<"\tPress 12 : delete a character"<<endl;
cout<<"\tPress 13 : delete by index and span"<<endl;
cout<<"\tPress 0 : exit"<<endl;
}
int str::len()
{
int i;
for(i=0;ch[i]!='\0';i++);
return i;
}
void str::copy(str s1)
{
int i;
for(i=0;s1.ch[i]!=0;i++)
{
ch[i]=s1.ch[i];
}
ch[i]='\0';
}
void str::read()
{
cin.getline(ch,200);
}
void str::display()
{
cout<<"\t -----> "<<ch;
}
void str::concat(str s1)
{
int i,j,l1,l;
l=len();
l1=s1.len();
for(i=l,j=0;j<l1;j++,i++)
{
ch[i]=s1.ch[j];
}
ch[i]='\0';
}
int str::substr(str s1)
{
int l1,f,c=0,index[50],t=0;
int i,j,l;
l=len();
l1=s1.len();
for(i=0;i<=l-l1;i++)
{
f=1;
for(j=0;j<l1;j++)
{
if(ch[i+j]!=s1.ch[j])
f=0;
}
if(f!=0)
{
c++;
index[t]=i;
t++;
}
}
if(c>0)
{
cout<<endl<<"yes at index value: ";
for(i=0;i<t;i++)
{
cout<<index[i]<<", ";
}
return index[0];
}
else
{
cout<<endl<<s1.ch<<" Not Found in "<<ch;
return -1;
}
}
int str::subStr(str s1)
{
int l1,f,c=0,index[50],t=0;
int i,j,l;
l=len();
l1=s1.len();
for(i=0;i<=l-l1;i++)
{
f=1;
for(j=0;j<l1;j++)
{
if(ch[i+j]!=s1.ch[j])
f=0;
}
if(f!=0)
{
c++;
index[t]=i;
t++;
}
}
if(c>0)
return index[0];
else
return -1;
}
int str::replace(str s1,str s2)
{
int l1,index=0,l2;
int i,count=0,j;
str app;
l1=s1.len();
l2=s2.len();
index=subStr(s1);
while(index!=-1)
{
//Separating the append part
count++;
app.ch[0]='\0';
for(i=index+l1,j=0;i<len();i++,j++)
{
app.ch[j]=ch[i];
}
app.ch[j]='\0';
//coping the replacement
for(i=index,j=0;j<l2;i++,j++)
{
ch[i]=s2.ch[j];
}
ch[i]='\0';
concat(app);
index=subStr(s1);
}
if(count)
return 1;
else
return 0;
}
int main()
{
str s,s1,s2,app;
int ch=1,i,j;
char c,buffer[10];
s.menu();
cout<<endl<<"\t";
while (ch!=0){
switch(ch)
{
case 1:cout<<"Enter The String: ";
s.read();
break;
case 2: s.display();
break;
case 3: cout<<"-----> Length Of the string is: "<<s.len();
break;
case 4: cout<<"Enter String to be copied: ";
s1.read();
s.copy(s1);
s.display();
break;
case 5: cout<<"Enter The append String: ";
app.read();
s.concat(app);
s.display();
break;
case 6: cout<<"Enter the substring:";
s1.read();
s.substr(s1);
break;
case 7: cout<<"Enter the string to be replaced: ";
s1.read();
cout<<"Enter the Replacement: ";
s2.read();
if(s.replace(s1,s2))
s.display();
else
cout<<"\t---->Substring Not Found";
break;
case 8: s.reverse();
s.display();
break;
case 9:cout<<"Enter String to compare: ";
s1.read();
if(s.compare(s1))
cout<<"\t----> Strings are Equal"<<endl;
else
cout<<"\t----> Strings are Not Equal"<<endl;
break;
case 10:cout<<"Enter The Character: ";
cin>>c;
s.display();
cout<<endl;
cout<<"\t ----> Character: "<<c<<" has occurred "<<s.frequency(c)<<" times";
break;
case 11:if(s.palindrome())
cout<<"\t----> String is Palindrome"<<endl;
else
cout<<"\t----> String is not Palindrome"<<endl;
break;
case 12:cout<<"Enter character: ";
cin>>c;
s=s.chardelete(c);
s.display();
break;
case 13:cout<<"Enter Starting Point: ";
cin>>i;
cout<<"Enter Span: ";
cin>>j;
s1=s.modify(i-1,j-1);
if (s1.len())
s1.display();
else
cout<<"\t---->NULL";
break;
case 0: cout<<endl<<"Exiting....";
break;
default:cout<<endl<<"!!!!!!Enter a valid Choice form Menu!!!!!!";
}
//s.menu();
cout<<endl<<"_______________________________________________";
cout<<endl<<"Enter Choice from Menu [Press 0 to exit]: ";
cin>>ch;
gets(buffer);
}
return 0;
}