In: Computer Science
Write a function called HowMany(), which counts the occurrences of the second argument which is a single character in the first argument which is a string. This function should have 2 arguments, the first one is a string and the second argument is a character. For example, the following function : i = count("helloyoutwo", 'o'); would return i= 3. Test your function in a complete code.
Language: c++
Hi...
This program is written and tested in jdoodle online compiler.
The program is well commented. If any doubt please comment below...
#include <iostream>
using namespace std;
// function definition
int HowMany(string str, char c) 
{ 
    // Count variable 
    int count = 0; 
  
    for (int i=0;i<str.length();i++) 
  
        // checking character in string 
        if (str[i] == c) 
            // if character is equal to the character in string
            count++; 
            
    // to return count of occurrances of character
    return count; 
} 
  
// main function to test the function
int main() 
{ 
    // calling the function 
    int i = HowMany("helloyoutwo", 'o');
    //to print out the count of occurrances of character 
    cout<< "Count is = "<< i;
    return 0; 
} 
screenshot:

Thank you...!