Ask Question
25 April, 11:20

Your program must define and call the following function that returns the number of times the input character appears in the input string. int CountCharacters (char userChar, string userString)

+4
Answers (1)
  1. 25 April, 15:02
    0
    The program to this question can be given as:

    Program:

    #include / /header file

    using namespace std; / /using name space.

    int CountCharacters (char userChar, string userString) / /define function.

    {

    int count = 0; / /define variable

    for (int i=0; i
    if (userString[i]==userChar) / /if block

    count++; / /increment the value.

    return count; / /return value.

    }

    int main () / /main method.

    {

    string userString = "Clanguage"; / /define variable and assign value.

    char userChar = 'a'; / /define variable and assign value.

    cout << CountCharacters (userChar, userString); / /calling function and print value.

    return 0;

    }

    Output:

    2

    Explanation:

    In the above C+ + programming language program we define a function that is "CountCharacters". Inside function parameters, we pass two parameters that are "userChar and userString". The userChar is a char variable that holds single character value and userString is a string variable that holds a string value. In the function, we define an integer variable that is "count". This variable counts the match value and returns its value. To counts values, we define a loop in a loop we use if statement that counts the value that how many time it occurs in a string value and return its value. In the main method, we define these variables ("userChar and userString") and assign values that are "a and Clanguage " and call the function and print function return value.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Your program must define and call the following function that returns the number of times the input character appears in the input string. ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers