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:
#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;
}
#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;
}
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.
#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;
}
#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;
}
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.
#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';
}
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.
#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);
}
Credit to Eibro (and Prelude).