1. Use std::transform
and tolower
or toupper
#include <algorithm> #include <string> #include <cctype> std::string& to_lower(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::tolower)); return str; } std::string& to_upper(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::toupper)); return str; } std::string str = "Chorlton-cum-Hardy"; to_lower(str); to_upper(str);
2. Use boost::algorithm::to_lower
or boost::algorithm::to_upper
#include <boost/algorithm/string.hpp> std::string str = "Adwick-le-Street"; boost::algorithm::to_lower(str); boost::algorithm::to_upper(str);
References:
Function template to_lower
Function template to_upper
Related
- How to split a string in C++
- How to find a substring in C++
- How to do string formatting in C++
- How to replace all occurrences of a character in a std::string
- How to do case-insensitive string comparison in C++
- How to concatenate a string and an int in C++
- How to trim a std::string in C++
- How to get a const char* or a char* from a std::string
- How to convert an int to a std::string in C++