The towupper()
function is a built-in function of C++. It helps convert wide characters to uppercase characters.
A wide character is like a normal character data type, except it represents the unicode character set instead of the ASCII character set. Hence, a wide character takes up 2-bytes of space as compared to the 1-byte taken by a normal character.
In order to represent an ASCII string as a wide character or string, “L” is used by attaching it to the beginning of the string. For example, L"Hello world!"
.
To use
towupper()
in C++, you must include thecwctype.h
header file.
#include <cwctype.h>
wint_t towupper( wint_t ch );
The towupper()
function takes a wide character as a parameter.
ch
: This is the wide character that is passed in order to be converted to uppercase.The function returns two values.
ch
has an uppercase version, then it is converted.ch
does not have an uppercase version, then no modification happens.In the code below, we create some wide characters and convert them to uppercase.
#include <cwctype>#include <iostream>using namespace std;int main() {wchar_t a = L'S'; // Not a wide characterwchar_t b = L'C'; // Not a wide characterwchar_t c = L'S'; // A wide character but not lowercasewchar_t d = L'c'; // A wide character and lowercase tooputwchar(towupper(a));putwchar(towupper(b));putwchar(towupper(c));putwchar(towupper(d));return 0;}
In the example below, we will create an array of wide characters, convert them to upper case, and print them to the console.
#include <cwchar>#include <cwctype>#include <iostream>using namespace std;int main(){wchar_t str[] = L"edpresson is awesome|!";wcout << L"The uppercase version of \"" << str << L"\" is ";for (int i = 0; i < wcslen(str); i++)// Function to convert the character// into the uppercase version, if existsputwchar(towupper(str[i]));return 0;}