tolower

Why putchar, toupper, tolower, etc. take a int instead of a char?

十年热恋 提交于 2019-11-27 03:51:45
问题 In C, strings are arrays of char ( char * ) and characters are usually stored in char . I noticed that some functions from the libC are taking as argument integers instead of a char. For instance, let's take the functions toupper() and tolower() that both use int . The man page says: If c is not an unsigned char value, or EOF, the behavior of these functions is undefined. My guess is that with a int , toupper and tolower are able to deal with unsigned char and EOF . But in fact EOF is in

Why can't “transform(s.begin(),s.end(),s.begin(),tolower)” be complied successfully?

百般思念 提交于 2019-11-27 00:52:43
Given the code: #include <iostream> #include <cctype> #include <string> #include <algorithm> using namespace std; int main() { string s("ABCDEFGHIJKL"); transform(s.begin(),s.end(),s.begin(),tolower); cout<<s<<endl; } I get the error: No matching function for call to transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator

Do I need to cast to unsigned char before calling toupper(), tolower(), et al.?

最后都变了- 提交于 2019-11-25 23:42:23
问题 A while ago, someone with high reputation here on Stack Overflow wrote in a comment that it is necessary to cast a char -argument to unsigned char before calling std::toupper and std::tolower (and similar functions). On the other hand, Bjarne Stroustrup does not mention the need to do so in the C++ Programming Language . He just uses toupper like string name = \"Niels Stroustrup\"; void m3() { string s = name.substr(6,10); // s = \"Stroustr up\" name.replace(0,5,\"nicholas\"); // name becomes

How to convert std::string to lower case?

谁都会走 提交于 2019-11-25 22:58:23
问题 I want to convert a std::string to lowercase. I am aware of the function tolower() , however in the past I have had issues with this function and it is hardly ideal anyway as use with a std::string would require iterating over each character. Is there an alternative which works 100% of the time? 回答1: Adapted from Not So Frequently Asked Questions: #include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), data.end(), data.begin(), []