Question

Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

Please write code in C++ and include all headers not bits/stdc:

17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header.

int count(const  string& s, char a)

For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the string.

(Occurrences of a specified character in a string)

Rewrite Programming Exercise 17.10 using a helper function to pass the substring high index to the function.

You need to define the following two functions. The second one is a recursive helper function.

int count(const string& s, char a)

int count(const string& s, char a, int high)

Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences of the character in the string.

Input:

Welcome welcome

e

Output:

Enter a string: Welcome welcome
Enter a character: e
e occurs 4 times in Welcome welcome
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the complete c++ that implements both the variations of the functios

//These are the required header file
#include <iostream>
#include <string>
using namespace std;

//Doing it with fuction requirment of first function
int count(string &thestring, char a)
{
    //if size is 0 simply return 0
    if (thestring.size() == 0)
        return 0;
    int thecount = 0;
    //if the first character matches the required char then increase count
    if (thestring[0] == a)
        thecount++;
    //Now make a substring using the substr function [pos, pos+len)
    string temp = thestring.substr(1, thestring.size() - 1);
    //Recursively call the function
    return thecount + count(temp, a);
}

int countfunction2(const string &s, char a, int high)
{
    //if we have gone past 0 then return 0
    if (high == -1)
        return 0;
    //If the high index is char is same as the required char then recursively call with +1 addition and do high-1
    if (s[high] == a)
        return countfunction2(s, a, high - 1) + 1;
    //Other wise simply do high-1
    return countfunction2(s, a, high - 1);
}

int main()
{
    string str = "Helloteehere";
    //Calling first function
    cout << countfunction2(str, 'e', 9);
    cout << "\n";
    //Calling second function
    str = "mynameisharrypotterrrrr";
    cout << count(str, 'r');
    return 0;
}

Here is the image of the code to help understand the indentation:

//These are the required header file #include <iostream> #include <string> using namespace std; == //Doing it with fuction re

OUTPUT:

D:\MyPrograms\C++>a 4 7 D:\MyPrograms\C++>

Add a comment
Know the answer?
Add Answer to:
Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT