Ask Question
20 November, 21:24

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)

+3
Answers (1)
  1. 20 November, 23:12
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers