How can I convert a char/string to upper or lower case?


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > How do I... (Level 2) > How can I convert a char/string to upper or lower case?

This item was added on: 2003/02/23

For single characters converting to upper/lower case is simple, use the functions toupper() and tolower() included in ctype.h (or cctype in C++)

For example:


/*
 * A C++ version to change the case of a letter
 */

#include <cctype> 
#include <iostream> 

int main(void)
{
   using std::cout;
   using std::endl;
   using std::toupper;
   using std::tolower;

   char letter = 'a';
   cout << "Before toupper: " << letter << endl;
   letter = toupper(letter);
   cout << "After toupper: " << letter << endl;
   letter = tolower(letter);
   cout << "After tolower: " << letter << endl;

   return 0;   
}
/*
  Output:
  Before toupper: a
  After toupper: A
  After tolower: a
*/



/*
 * A C version to change the case of a letter
 */

#include <stdio.h> 
#include <ctype.h> 

int main(void)
{
   char letter = 'a';
   
   printf ("Before toupper: %c\n", letter);
   letter = toupper(letter);
   printf ("Before toupper: %c\n", letter);
   letter = tolower(letter);
   printf ("Before toupper: %c\n", letter);

   return 0;   
}
/*
  Output:
  Before toupper: a
  After toupper: A
  After tolower: a
*/


For strings, it's a little more compilcated. First, it depends on what type of 'string' you're using. If it's traditional C-style strings (character arrays) then you'll need to iterate through the array and call tolower() or toupper() for each element.


/*
 * A C++ Version showing how to iterate an array, converting character case
 */

#include <cctype> 
#include <iostream> 

using std::cout;
using std::endl;

int main(void)
{
   char hello[] = "Hello World";
   
   cout << "Before conversion: " << hello << endl;

   for (char *iter = hello; *iter != '\0'; ++iter)
   {
       *iter = std::tolower(*iter);
       ++iter;
   }

   cout << "After conversion: " << hello << endl;

   return 0;   
}
/*
   Output:
   Before conversion: Hello World
   After conversion: hello world
*/



/*
 * A C Version showing how to iterate an array, converting character case
 */

#include <stdio.h> 
#include <ctype.h> 

int main(void)
{
   char hello[] = "Hello World";
   char *p;
   
   printf ("Before conversion: %s\n", hello);

   for (p = hello; *p != '\0'; ++p)
   {
       *p = tolower(*p);
   }

   printf ("After conversion: %s\n", hello);

   return 0;   
}
/*
   Output:
   Before conversion: Hello World
   After conversion: hello world
*/


For C++-style strings of the std::string type, things are essentially the same. You need to iterate through each character and call tolower() or toupper(). Luckily, the STL includes an algorithm which does most of the work for us.


/*
 * A C++ Version showing how to use a std::string object and the STL
 */

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

struct to_lower {
  int operator() ( int ch )
  {
    return std::tolower ( ch );
  }
};

int main()
{
  std::string hello = "Hello World";

  std::cout<<"Before conversion: "<< hello <<'\n';
  std::transform(hello.begin(), hello.end(), hello.begin(), to_lower());
  std::cout<<"After conversion: "<< hello <<'\n';
}

/*
   Output:
   Before conversion: Hello World
   After conversion: hello world
*/


The transform() algorithm defined in <algorithm> is used to iterate through all elements starting at the address pointed to by the first argument, up to the address pointed to by the second argument. The result of the transformation is placed into the memory address starting wherever the third argument points, and in this example, that's the first element of the array again. The last argument is a pointer to the function or function object that will perform the transformation, in this example tolower(). A function object is required to wrap the function that we want because both cctype and iostream define a tolower function.

Finally, in case you're wondering, if you pass a character that is not a-z, A-Z to either of these two functions, nothing bad happens, the characters just get left untouched. Here is a short example showing a function to convert any characters it can to uppercase.


/*
 *  A short function to convert an array to uppercase,
 *  showing that we don't need to worry about non-character
 *  elements.
 */
#include <stdio.h> 
#include <ctype.h> 

void upstr(char *s)
{
  char  *p;

  for (p = s; *p != '\0'; p++) 
    *p = (char) toupper(*p);
}

int main(void)
{
  char  mystring[] = "Some 1234 text 5678 here!";
  puts(mystring);
  upstr(mystring);
  puts(mystring);
  return(0);
}

/*
 * Program output:
 Some 1234 text 5678 here!
 SOME 1234 TEXT 5678 HERE!
 *
 */

Credit to Eibro (and Prelude).

Script provided by SmartCGIs