Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::tolower

 

std::tolower is a function defined in the header file cctype.h to convert a char to lower case.

 

 

 

 

 

Example: StrToLower using a for loop

 

StrToLower can be implemented using a for loop, but prefer algorithm calls over hand-written loops [1][2]. View Exercise #9: No for-loops for other ways of replacing for-loops by algorithms.

 

#include <algorithm>
#include <cctype>
#include <string>

//From http://www.richelbilderbeek.nl/CppStrToLower.htm
const std::string StrToLower(std::string s)
{
  std::transform(s.begin(), s.end(), s.begin(),std::ptr_fun(std::tolower));
  return s;
}

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops.
  2. Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict