Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

C++ Basic Algorithm Exercises: Check if a given string contains between 2 and 4 'z' character.

Flowchart:

Flowchart: Check if a given string contains between 2 and 4 'z' character.

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?