C++ wctrans()

The wctrans() function in C++ returns a value of type wctrans_t that corresponds to the transformation.

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

wctrans() prototype

wctrans_t wctrans(const char* str);

The wctrans() function takes a C string as its argument and returns a value of type wctrans_t that is used for mapping a wide character.

wctrans() Parameters

  • str: C string specifying the desired transformation.

wctrans() Return value

  • The wctrans() function returns a wctrans_t object that can be used with towctrans() for mapping wide characters.
  • If str doesn't provide a mapping supported by the current C locale, it returns zero.

Example: How wctrans() 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
Did you find this article helpful?