Answered step by step
Verified Expert Solution
Link Copied!

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 count_char.
#include
#include
using namespace std;
/**
Gets the number of times the character c appears in the string.
@param str any string
@param c the single-character string to search for
@return the number of times c appears in str
*/
int count_char(const string& str, char c)
{
int count =0;
for (int i =0; i str.length(); i++)
{
if (str[i]== c){ count++; }
}
return count;
}
int main()
{
string str;
string c;
getline(cin, str);
cin >> c;
cout count_char(str, c) endl;
return 0;
}
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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. Discuss the key components of behavior modeling training.

Answered: 1 week ago