wctrans() function in C/C++

This wctrans() in C/C++ is specified in header file cwctype.h and returns a value of type wctrans_t that corresponds to the transformation. A specific locale can accept multiple transformations for characters. Some following transformation are recognized as follows :

  • “tolower” -> to lowercase | towlower
  • “toupper” -> to uppercase | towupper

Syntax:

wctrans_t wctrans( const char* string )

Parameters: The function accepts one mandatory parameter string which specifies a string identifying a character transformation.

Return value : The function returns two value as below:

  • If current locale doesn’t provide a mapping then it returns zero.
  • Otherwise, It returns object that can be used with towctrans() for mapping wide characters.

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// towctrans() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initilize the string
    wchar_t string[] = L"GeeksForGeeks";
    wcout << L"Initial string -> " << string << endl;
  
    wctype_t first = wctype("lower");
    wctype_t second = wctype("upper");
  
    // traverse each character and convert
    // lower case to upper and upper to lower
    for (int i = 0; i < wcslen(string); i++) {
        if (iswctype(string[i], first))
            string[i] = towctrans(string[i], wctrans("toupper"));
        else if (iswctype(string[i], second))
            string[i] = towctrans(string[i], wctrans("tolower"));
    }
  
    // print the string after transformation
    wcout << L"After transformation -> " << string << endl;
  
    return 0;
}

chevron_right


Output:

Initial string -> GeeksForGeeks
After transformation -> gEEKSfORgEEKS

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// towctrans() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initilize the string
    wchar_t string[] = L"gfg";
    wcout << L"Initial string -> " << string << endl;
  
    wctype_t first = wctype("lower");
    wctype_t second = wctype("upper");
  
    // traverse each character and convert
    // lower case to upper and upper to lower
    for (int i = 0; i < wcslen(string); i++) {
        if (iswctype(string[i], first))
            string[i] = towctrans(string[i], wctrans("toupper"));
        else if (iswctype(string[i], second))
            string[i] = towctrans(string[i], wctrans("tolower"));
    }
  
    // print the string after transformation
    wcout << L"After transformation -> " << string << endl;
  
    return 0;
}

chevron_right


Output:

Initial string -> gfg
After transformation -> GFG


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.