Think about it for a while. Come up with a way that allows you to do all three checks in one iteration.
Also, here's a neat trick:
std::string s = "Hello World";
std::transform(s.begin(), s.end(), s.begin(), &std::tolower);
// s is now "hello world".
std::transform is a function that takes a sequence denoted by two iterators, and an iterator to where to store the result and finally a functor which to call for every element in the range.
So simply pit, it takes every element in the sequence, calls the functor and then stores back the result beginning at the iterator denoted by the third argument.
It may seem a little advanced, but consider it as a challenge. You can use it to learn some new C++ concepts.
An iterator can be seem as a position. A functor can be seen as a function (ie a function is a functor, but a functor may be more than that; you'll get to that eventually).
This is more or less what i had in mind:
But no, i don't think there is any advantage to this approach. If anything i find the code that the OP ended up using to be simpler and therefore better.Code:unsigned int count = 0; std::size_t found = str.find("gc"); while(found != std::string::npos) { found = str.find("gc", found + 2); ++count; }
How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.