-
Notifications
You must be signed in to change notification settings - Fork 2
/
2.STRING_OPERATIONS.cpp
207 lines (198 loc) · 3.92 KB
/
2.STRING_OPERATIONS.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
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
#include<iostream>
#include<stdio.h>
using namespace std;
class strings
{
private:
char s1[30];
char s2[30];
char s3[30];
char s4[30];
int i,j,k,len;
char a,b,c;
public:
void enter_string()//strlen()
{
len=0;
fflush(stdin);
cout<<"Enter string : ";
gets(s1);
for(i=0;s1[i]!='\0';i++)
len++;
cout<<len;
}
//----------------------------------------------------------------------
void display_string()
{
cout<<"String : "<<s1<<endl;
}
//----------------------------------------------------------------------
void frequency_char()//strchr()
{
int cnt=0;
cout<<"Enter character to find : ";
cin>>a;
for(i=0;s1[i]!='\0';i++)
{
if(s1[i] == a)
cnt++;
}
cout<<"Character "<<a<<" has occur "<<cnt<<" times"<<endl;
}
//----------------------------------------------------------------------
void delete_char()//delchar(string,n,pos)n=no.of char to delete pos=position)
{
cout<<"Enter character to delete : ";
cin>>a;
for(i=0,j=0;s1[i]!='\0';i++,j++)
{
if(s1[i] == a)
i++;
s2[j] = s1[i];
}
//display
j++;
s2[j]='\0';
cout<<"Deleted string is "<<s2;
}
//----------------------------------------------------------------------
void delete_sub()
{
int s , l;
cout<<"Enter start and length of string : ";
cin>>s>>l;
for(i=0,j=0;s1[i]!='\0';i++,j++)
{
if(i == s)
break;
s2[j]=s1[i];
}
for(i=s+l;s1[i]!='\0';i++,j++)
{
s2[j]=s1[i];
}
cout<<"New string : "<<s2;
}
//----------------------------------------------------------------------
void replace_sub()//strstr()
{
int m;
char newstr[30];
char rstr[30];
char sstr[30];
int rlen=0;
int slen=0;
cout<<"Enter the string to be search : ";
cin>>sstr;
cout<<"Enter the replace string : ";
cin>>rstr;
for(i=0;rstr[i]!='\0';i++,rlen++){
}
for(i=0;sstr[i]!='\0';i++,slen++){
}
//cout<<"\nslen = "<<slen<<endl<<"rlen = "<<rlen;
j=0;
for(i=0;i<len;)//remember len not '\0'
{
if(s1[i]==sstr[j])
{
i++;
j++;
if(j == slen)
{
for(k=0;k<i-slen;k++)
{
newstr[k]=s1[k];
}
for(m=0;m<rlen;m++,k++)
{
newstr[k]=rstr[m];
}
//
if(k>=i)
{
m=i;
for(;s1[m]!='\0';m++,k++)
{
newstr[k]=s1[m];
}
}
else
{
m=k;
for(;s1[i]!='\0';i++,k++)
{
newstr[k]=s1[i];
}
}
//give it here break
}
newstr[k]='\0';
// break;
}
else
{
i++;
}
}
cout<<endl<<"NEW STRING : "<<newstr;
}
//----------------------------------------------------------------------
void palindrome()
{
len--;
for(i=0;s1[i]!='\0';i++,len--)
{
if(s1[i]!=s1[len])
{
cout<<"String not a Palindrome";
return;
}
}
cout<<"String is palindrome";
}
//----------------------------------------------------------------------
};
main()
{
strings s;
int op=-1;
while(op!=0)
{
cout<<"\n\n***********MENU***************"<<endl;
cout<<"1 : ENTER STRING"<<endl;
cout<<"2 : FREQUENCY OF CHARACTER"<<endl;
cout<<"3 : DELETE CHARACTER"<<endl;
cout<<"4 : DELETE SUBSTRING"<<endl;
cout<<"5 : REPLACE SUBSTRING"<<endl;
cout<<"6 : CHECK PALINDROME"<<endl;
cout<<"7 : EXIT"<<endl;
cout<<"********************************"<<endl;
cout<<"\nENTER CHOICE : ";
cin>>op;
switch(op)
{
case 1:
s.enter_string();
break;
case 2:
s.frequency_char();
break;
case 3:
s.delete_char();
break;
case 4:
s.delete_sub();
break;
case 5:
s.replace_sub();
break;
case 6:
s.palindrome();
break;
case 7:
op=0;
break;
}
}
}