Skip to content

Instantly share code, notes, and snippets.

@Liam0205
Created December 13, 2017 11:26
Show Gist options
  • Save Liam0205/744af745eee9580239d843f0ff02e20d to your computer and use it in GitHub Desktop.
Save Liam0205/744af745eee9580239d843f0ff02e20d to your computer and use it in GitHub Desktop.
transform std::string to lower case or upper case.
$ ./a.out
HELLO WORLD!
hello world!
#include <string>
#include <algorithm>
namespace std {
const auto str_tolower = [](std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
return s;
};
const auto str_toupper = [](std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return toupper(c); });
return s;
};
} // namespace std
#include <iostream>
#include <string>
#include "str_transform.hpp"
int main() {
std::string lowercase{"hello world!"};
std::string uppercase{"HELLO WORLD!"};
std::cout << std::str_toupper(lowercase) << std::endl;
std::cout << std::str_tolower(uppercase) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment