Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) std::toupper

 

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

 

 

 

 

 

Example: StrToUpper using a for loop

 

StrToUpper 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 <cctype>
#include <string>

//From http://www.richelbilderbeek.nl/CppStrToUpper.htm
const std::string StrToUpper(std::string s) //Not the preferred way
{
  const int sz = static_cast<int>(s.size());
  for(int i=0; i!=sz; ++i)
  {
    s[i] = std::toupper(s[i]);
  }
  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