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
  • Use Java to answer the question (Occurrences of a specified character) Write a method that finds...

    Use Java to answer the question (Occurrences of a specified character) Write a method that finds the number of occurrences of a special character in a string using the following header: public static int count (String str, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string followed by a character then displays the number of occurrences of the character in the string. In addition to the requirements described in...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ā–  A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • //Write a recursive function that, given an input string str, replaces all occurrences of a character...

    //Write a recursive function that, given an input string str, replaces all occurrences of a character a by a character b in str. (C++) void replace_chr(char *str, char a, char b){ }

  • In C++ (HexFormatException) Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if...

    In C++ (HexFormatException) Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if the string is not a hex string . Define a custom exception class named HexFormatException. Write a test program that prompts the user to enter a hex number as a string and displays the number in decimal. If the function throws an exception, display "Not a hex number". Included is the hex2Dec Function #include <iostream> #include <string> #include <cctype> using namespace std; // Converts...

  • Please answer in Python 3! 16.2** (Maximum increasingly ordered subsequence) Write a program that prompts the...

    Please answer in Python 3! 16.2** (Maximum increasingly ordered subsequence) Write a program that prompts the user to enter a string and displays the maximum increasingly ordered subsequence of characters. Analyze the time complexity of your program. Here is a sample run: Enter a string: Welcome Maximum consecutive substring is ['W', 'e', 'l', 'o']

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • (Longest common prefix, C-string, loop, char comparison) Write the prefix function to find the longest prefix...

    (Longest common prefix, C-string, loop, char comparison) Write the prefix function to find the longest prefix of two strings using C-strings with the following header: void prefix( const char s1[ ], const char s2[ ], char commonPrefix[ ]) Write a test program that prompts the user to enter two C-strings and displays their common prefix. Sample run :- String 1: Programming is fun String 2: Program logic The common prefix is program.

  • (C++ only) Write a function that returns a decimal number from a binary string. The function...

    (C++ only) Write a function that returns a decimal number from a binary string. The function header is as follows: int bin2Dec(const string& binaryString) For example, bin2Dec("10001") returns 17. Write a test program that prompts the user to enter a binary number as a string and displays its decimal equivalent value Sample Input: 1110100110101 Sample Output: Enter a bianry number: 1110100110101 7477

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