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
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:
OUTPUT:
Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...
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 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...
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...
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...
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...
//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){ }
[10 marks] Write a recursive method, reverse, that displays a string in a reverse order. For example, reverse("UBC-O") displays O-CBU. Use a helper method to improve the performance of your program. Write a test program that prompts the user to enter a string and displays its reversal. MUST HAVE A HELPER METHOD TO IMPROVE PERFORMANCE. FOR JAVA THANK YOU!
Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...
Write a recursive function that uses the strchr() function to display the number of occurrences of a character inside a string. Create a C program to read a string of less than 10 characters and a character and calls the function.