How do I convert a string to uppercase or lowercase? This is something that often needs to be done in string matching, but the standard library of C + + does not provide the ability to turn std::string into uppercase and lowercase, only to provide the ability to turn char into uppercase (toupper) and lowercase (tolower).
But we can use the STL transform with Toupper/tolower, complete std::string conversion Large (small) write function, also see the power of template programming, a transform function, can be applied to any type, and as long as they provide functions, You can complete any transform action.
C++
#include <iostream> #include <string> #include <cctype> #include <algorithm>using namespace std ; int main () { string s = "Clare"; ToUpper Transform (S.begin (), S.end (), S.begin (),:: ToUpper); ToLower //transform (S.begin (), S.end (), S.begin (),:: ToLower); cout << s << endl;}
C
#include <stdio.h> #include <ctype.h>int main () { char s[] = "Clare"; int i =-1; while (s[i++]) s[i] = ToUpper (S[i]); S[i] = ToLower (S[i]); Puts (s); }
Reprint Link
String Conversion case (c + +)