-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss3_StringOperations.cpp
More file actions
138 lines (108 loc) · 2.65 KB
/
Copy pathAss3_StringOperations.cpp
File metadata and controls
138 lines (108 loc) · 2.65 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
#include <iostream>
#include <cstring>
using namespace std;
class Str {
char s[100];
public:
void input(){
cout<<"Enter string: ";
cin>>s;
}
// 1. Frequency
void freq(){
char ch;
int count=0;
cout<<"Enter character: ";
cin>>ch;
for(int i=0; s[i]!='\0'; i++){
if(s[i]==ch)
count++;
}
cout<<"Frequency = "<<count<<endl;
}
// 2. Substring
void substr(){
int pos,len;
char temp[100];
cout<<"Enter start and length: ";
cin>>pos>>len;
int j=0;
for(int i=pos; i<pos+len && s[i]!='\0'; i++){
temp[j++] = s[i];
}
temp[j]='\0';
cout<<"Substring = "<<temp<<endl;
}
// 3. Remove character
void removeChar(){
char ch;
cout<<"Enter character: ";
cin>>ch;
int j=0;
for(int i=0; s[i]!='\0'; i++){
if(s[i]!=ch){
s[j++] = s[i];
}
}
s[j]='\0';
cout<<"Updated string = "<<s<<endl;
}
// 4. Replace substring
void replace(){
char w[50], x[50], result[100];
cout<<"Enter substring w: ";
cin>>w;
cout<<"Enter new string x: ";
cin>>x;
int i=0,j=0;
while(s[i]!='\0'){
int k=0;
while(w[k]!='\0' && s[i+k]==w[k]){
k++;
}
if(w[k]=='\0'){ // match
for(int t=0; x[t]!='\0'; t++){
result[j++] = x[t];
}
i += k;
}
else{
result[j++] = s[i++];
}
}
result[j]='\0';
strcpy(s,result);
cout<<"Updated string = "<<s<<endl;
}
// 5. Palindrome
void palindrome(){
int i=0;
int j=strlen(s)-1;
int flag=1;
while(i<j){
if(s[i]!=s[j]){
flag=0;
break;
}
i++;
j--;
}
if(flag) cout<<"Palindrome\n";
else cout<<"Not Palindrome\n";
}
};
int main(){
Str obj;
int ch;
obj.input();
do{
cout<<"\n1.Frequency\n2.Substring\n3.Remove\n4.Replace\n5.Palindrome\n6.Exit\n";
cin>>ch;
if(ch==1) obj.freq();
else if(ch==2) obj.substr();
else if(ch==3) obj.removeChar();
else if(ch==4) obj.replace();
else if(ch==5) obj.palindrome();
}while(ch!=6);
return 0;
}