It is still possible to do this with count_if (in one line of code, no less), however as this is for a homework assignment, I doubt it will be acceptable that way. Nonetheless, in the name of education, I give an "advanced" solution:
1 2 3 4 5 6 7 8 9
#include <algorithm>
#include <ctype>
#include <string.h>
#include <boost/bind.hpp>
int letterfrequency( char s[], char letter ) {
return std::count_if( s, s + strlen( s ),
boost::bind( tolower, _1 ) == boost::bind( tolower, letter ) );
}
Or very slightly better performance, but 2 lines
1 2 3 4 5 6 7 8 9 10
#include <algorithm>
#include <ctype>
#include <string.h>
#include <boost/bind.hpp>
int letterfrequency( char s[], char letter ) {
int as_lower_case = tolower( letter );
return std::count_if( s, s + strlen( s ),
boost::bind( tolower, _1 ) == as_lower_case );
}