C++ Method question. So bascially I am making code that has a header file and a
ID: 3909064 • Letter: C
Question
C++ Method question.
So bascially I am making code that has a header file and a regualr cpp file, I will also be using classes and placing my private parts in the header file while my method parts are in the cpp file. I need to implement a method that will take in a string of words ( WordType ) and return the number of occurence of the key.
So to make a little more clear
Words.h(Header file content example)
Using WordType= std::string;
size_t countup(const WordType&) const;
I need to create
size_t WordMap::countup() {
}
which will go with my other methods.
Explanation / Answer
(Ans-)
;CODE
// C++ program to count occurrences of a given
// character
#include <iostream>
#include <string>
using namespace std;
// Function that return count of the given
// character in the string
int count(string s, char c)
{
// Count variable
int res = 0;
for (int i=0;i<s.length();i++)
// checking character in string
if (s[i] == c)
res++;
return res;
}
// Driver code
int main()
{
string str= "geeksforgeeks";
char c = 'e';
cout << count(str, c) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.