#include <iostream>
#include <string>
#include <cctype> // toupper // tolower
#include <algorithm>
std::string toTitleCase(std::string s) {
bool newWord = true;
std::transform(s.begin(), s.end(), s.begin(),
[&](char c) {
unsigned char uc = static_cast<unsigned char>(c);
if (std::isspace(uc)) {
newWord = true;
return c; // still char
}
if (newWord) {
newWord = false;
return static_cast<char>(std::toupper(uc));
}
return static_cast<char>(std::tolower(uc));
});
return s;
}
int main() {
std::string s = "hELLo woRLD from c++";
std::cout << toTitleCase(s) << "\n";
}
/*
run:
Hello World From C++
*/