CPP/Strings/Case Conversion

From ProgrammingExamples
< CPP
Revision as of 10:29, 26 June 2010 by Bench (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  1. include <string>
  2. include <algorithm> // transform
  3. include <functional> // ptr_fun
  4. include <cctype> // toupper, tolower
  5. 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;

}