Question

In: Computer Science

Write a function called HowMany(), which counts the occurrences of the second argument which is a...

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++

Solutions

Expert Solution

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...!


Related Solutions

Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order. input: line1:number of figures line2:number Sample Input 5 -3 100 -1 -2 -1 Sample Output -3 1 -2 1 -1 2 100 1
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Add a new function that takes a phrase as an argument and counts each unique word...
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index. Assumptions and restrictions: The function must return a number. If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements. You can assume that the argument...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT