Created
December 13, 2017 11:26
-
-
Save Liam0205/744af745eee9580239d843f0ff02e20d to your computer and use it in GitHub Desktop.
transform std::string to lower case or upper case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./a.out | |
HELLO WORLD! | |
hello world! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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