Open In App

wctrans() function in C/C++

Last Updated : 05 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 
 

CPP




// C++ program to illustrate
// towctrans() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // initialize 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;
}


Output: 

Initial string -> GeeksForGeeks
After transformation -> gEEKSfORgEEKS

 

Program 2 : 
 

CPP




// C++ program to illustrate
// towctrans() function
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // initialize 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;
}


Output: 

Initial string -> gfg
After transformation -> GFG

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads