How can I count the number of occurrences of a user inputed element in a string? Example:
Input: adseddsdf Input: a Output: 1
How can I count the number of occurrences of a user inputed element in a string? Example:
Input: adseddsdf Input: a Output: 1
You can count it yourself or use stl
#include <algorithm>
#include <string>
int main()
{
constexpr char chFind = 'a';
std::string str = "abcabca";
// first solution
size_t num1 = 0;
for(size_t i = 0; i < str.size(); i++)
if (str[i] == chFind)
num1++;
// second solution
size_t num2 = std::count(str.begin(), str.end(), chFind);
return 0;
}