C++ wrapper function for Python's "list.count([character])" method?
When STR is empty, STR.size() returns 0u. As a result, STR.size()-1 becomes maximum value of unsigned size type. i++ &lt; STR.size()-1. This will lead the function to crash.

Also your function will not count CHAR of the first character of STR.

You may want to use standard library.

std::count(begin, end, CHAR)  is just what you are looking for.

for example,

#include &lt;algorithm&gt;
#include &lt;string&gt;

...
char CHAR = 'l';
std::string STR("Hello World!");
ptrdiff_t counter = std::count(STR.begin(), STR.end(), CHAR);
...