string conversion case (C + +)

Source: Internet
Author: User

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 + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.