Question: C++ please Write a program whose input is a character followed by a string of words, and whose output is the # of times that

C++ please

Write a program whose input is a character followed by a string of words, and whose output is the # of times that character appears in the string. Example: if the input is:

n Monday is Not fine 

the output is:

2 

Note that case matters, e.g. 'N' was not counted since it's different than 'n'. Another example: if the input is:

z Today is Monday 

then the output is:

0 

Note that the main program is written for you, and cannot be modified. Your job is to write the function countAppearances defined in "util.cpp"; you can select this file from the drop-down list above the editing window. This function is called by main() to count the # of appearances, which is then output. You are free to implement the function however you want, but as practice try to implement using string's .find() member function, documented here. The documentation shows four ways to call the function: use the 4th approach that takes a character and a position. Note you'll need to call .find() repeatedly using a loop to count all appearances.

If you prefer to write your own loop to count appearances, note that you may get a warning if your loop variable is of type int. Since indexing is always 0 or positive, the correct type is an unsigned int, or properly written as size_t. Example:

for (size_t i = 0; ) {  }

main.cpp

// // HW #02-1: program to input a character and a string of words, and output // the # of appearances of that character in the string of words. //

#include #include

using namespace std;

// function declaration: int countAppearances(char c, string words);

int main() { char c; string words;

cout << "Please enter character followed by a string of words> "; cin >> c; getline(cin, words); cout << endl; cout << "# of appearances: " << countAppearances(c, words) << endl;

return 0; }

util.cpp

/*util.cpp*/

#include #include

using namespace std;

// // countAppearances: // // returns the # of appearances of c in words. // int countAppearances(char c, string words) { // // TODO: call .find() repeatedly, or search yourself: // size_t found = words.find(c); // found=words.find(c); if (found != c){ for (int i = 0; i < 1000; i++) { return i; // cout<

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!