#include <string> #include <algorithm> // ... std::string buf("hello world!"); int spaces = std::count_if(buf.begin(), buf.end(), isspace);