C++ Exercises: Check if a given string contains between 2 and 4 'z' character
C++ Basic Algorithm: Exercise-22 with Solution
Write a C++ program to check if a given string contains between 2 and 4 'z' character.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
bool test(string str)
{
int ctr = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == 'z')
{
ctr++;
}
}
return ctr > 1 && ctr < 4;
}
int main()
{
cout << test("frizz") << endl;
cout << test("zane") << endl;
cout << test("Zazz") << endl;
cout << test("false") << endl;
return 0;
}
Sample Output:
1 0 1 0
Pictorial Presentation:
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to find the larger value from two positive integer values that is in the range 20..30 inclusive, or return 0 if neither is in that range.
Next: Write a C++ program to check if two given non-negative integers have the same last digit.
What is the difficulty level of this exercise?