-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCountRemoveReplacehi.cpp
More file actions
136 lines (101 loc) · 2.83 KB
/
CountRemoveReplacehi.cpp
File metadata and controls
136 lines (101 loc) · 2.83 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
/*
Count Remove And Replace 'hi'
Take as input a string.
Write a recursive function which
(a) Counts the number of times 'hi' appears in the string. Print the value returned.
(b) Removes all 'hi' in the string. Print the value returned.
(c) Replaces all 'hi' in the string with 'bye'. Print the value returned.
part(b) and part(c) cannot be done simaltaneously.
If part(b) is followed first, then there wil be no 'hi' in the string,
hence there will be nothing to replace 'bye' with.
and similarly if part(c) is followed first, then there will be no 'hi' in the string,
and therefore cannot be removed.
*/
#include<iostream>
using namespace std;
int main()
{
char str[30];
int len, x, countHI(char [], int), ch;
void remov(char [], int), replc(char [], int);
cout<<"Enter string :\n";
gets(str);
for(len=0; str[len]!='\0'; ++len);
x=countHI(str,len); //part (a)
cout<<endl<<"Number of 'hi' in the string are : "<<x;
cout<<endl<<"\n\nPart(b) : Removes all 'hi' in the string.\n";
cout<<"Part(c) : Replaces all 'hi' in the string with 'bye'.\n";
cout<<endl<<"Enter your choice ( 1 or 2 ): ";
cin>>ch;
if(ch==1) //Part(b)
{
remov(str,len);
cout<<endl<<"\nAfter removing 'hi's from the string :\n";
puts(str);
}
else if(ch==2) //part (c)
{
replc(str,len);
cout<<endl<<"\nAfter replacing 'hi' with 'bye', the string becomes :\n";
puts(str);
}
else
cout<<endl<<"Wrong choice !!!"<<endl;
return 0;
}
int i=0, count=0;
int countHI(char str[], int len)
{
if(i<len-1)
{
if(str[i]=='h'&&str[i+1]=='i')
++count;
++i;
countHI(str,len);
}
return count;
}
int l=0;
void remov(char str[], int len)
{
if(l<len-1)
{
if(str[l]=='h'&&str[l+1]=='i')
{
int k=1;
while(k<=2)
{
for(int j=l; j<len; ++j)
str[j]=str[j+1];
++k;
--len;
}
remov(str,len);
}
++l;
remov(str,len);
}
}
int m=0;
void replc(char str[], int len)
{
if(m<len-1)
{
if(str[m]=='h'&&str[m+1]=='i')
{
int k=1, n=m;
while(k<2)
{
len+=1;
for(int j=len-1; j>m; --j)
str[j]=str[j-1];
++k;
}
str[n]='b';
str[n+1]='y';
str[n+2]='e';
}
++m;
replc(str,len);
}
}