C++ towctrans()

The towctrans() function in C++ transforms a given wide character according to a specified transformation.

The towctrans() function is defined in <cwctype> header file.

towctrans() prototype

wint_t towctrans(wint_t wc, wctype_t desc);

The towctrans() function applies a transformation to the wide character wc specified by desc.


towctrans() Parameters

  • wc: The wide character to be transformed.
  • desc: The transformation which is obtained from a call to wctrans().

towctrans() Return value

  • The towctrans() function returns non zero value if wc has the property specified by desc, otherwise returns zero.

Example: How towctrans() function works?

#include <cwctype>
#include <iostream>
#include <cwchar>
#include <clocale>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.UTF-8");
	wchar_t str[] = L"Ŝŵitĉhiňģ Ćăse";
	wcout << L"Before transformation" << endl;
	wcout << str << endl;

	for(int i=0; i<wcslen(str); i++)
	{
		if (iswctype(str[i], wctype("lower")))
		str[i] = towctrans(str[i], wctrans("toupper"));
		else if (iswctype(str[i], wctype("upper")))
		str[i] = towctrans(str[i], wctrans("tolower"));
	}

	wcout << L"After transformation" << endl;
	wcout << str << endl;
	
	return 0;
}

When you run the program, the output will be:

Before transformation
Ŝŵitĉhiňģ Ćăse
After transformation
ŝŴITĈHIŇĢ ćĂSE