-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Hi.cpp
More file actions
36 lines (28 loc) · 826 Bytes
/
Count_Hi.cpp
File metadata and controls
36 lines (28 loc) · 826 Bytes
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
#include <iostream>
#include <string>
using namespace std;
// Function to count the number of times "hi" appears in the string
// excluding cases where 'x' is immediately before "hi"
int countHi(string s) {
// Base case
if(s.length() < 2) {
return 0;
}
// If 'x' is immediately before "hi", skip it and continue recursion
if(s[0] == 'x' && s[1] == 'h') {
return countHi(s.substr(2));
}
// If "hi" is found, increment count and continue recursion
if(s.substr(0, 2) == "hi") {
return 1 + countHi(s.substr(1));
}
// Otherwise, continue recursion
return countHi(s.substr(1));
}
int main() {
string str;
cout << "Enter the string: ";
getline(cin, str);
cout << countHi(str);
return 0;
}