Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Instead of counting the number of spaces in a string, it might be more useful to generalize a function so that it counts any character.
Instead of counting the number of spaces in a string, it might be more useful to generalize a function so that it counts any
character. Write the function countchar.
#include
#include
using namespace std;
Gets the number of times the character c appears in the string.
@param str any string
@param c the singlecharacter string to search for
@return the number of times c appears in str
int countcharconst string& str char c
int count ;
for int i ; i strlength; i
if stri c count;
return count;
int main
string str;
string c;
getlinecin str;
cin c;
cout countcharstr c endl;
return ;
Failed to compile
Note: Although the reported line number is in the uneditable part of the code, the error actually exists in your code. Tools often
don't recognize the problem until reaching a later line.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started