Difference between revisions of "CPP/Strings/Case Conversion"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(C++ upper/lower case string conversion)
 
 
Line 1: Line 1:
 +
<source lang="cpp">
 
#include <string>
 
#include <string>
 
#include <algorithm>    // transform
 
#include <algorithm>    // transform
Line 21: Line 22:
 
     std::cout << lewis << std::endl;
 
     std::cout << lewis << std::endl;
 
}
 
}
 +
</source>

Latest revision as of 10:30, 26 June 2010

#include <string>
#include <algorithm>    // transform
#include <functional>   // ptr_fun
#include <cctype>       // toupper, tolower
#include <iostream>
 
int main()
{
    std::string clive = "Clive Cherishes Capitals",
                lewis = "Lewis LOVES Lowercase";
 
    // transform each character in 'clive' with 'toupper'
    std::transform(clive.begin(), clive.end(), clive.begin(),
                   std::ptr_fun<int,int>(std::toupper) );
 
    // transform each character in 'lewis' with 'tolower'
    std::transform(lewis.begin(), lewis.end(), lewis.begin(),
                   std::ptr_fun<int,int>(std::tolower) );
 
    std::cout << clive << std::endl;
    std::cout << lewis << std::endl;
}