Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wctrans in C?

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The wctrans function is used to create a character transformation in C. The transformation can convert a wide character from upper case to lower case or vice versa.

The wctype.h and wcharlibraries need to be included to use the wctrans function and declare a wide character string, respectively.

#include <wctype.h>
#include <wchar.h>

Syntax

wctrans_t wctrans (const char* property);

Parameters

The wctrans function takes in one argument:

  • property – Pointer to a character array that determines the transformation property. The property can be “touppper”. This converts the character from lower case to upper case, or it can be “tolower”, which converts the given character from upper case to lower case.

Return value

The wctrans function returns a value of type wctrans_t, passed as input to the towctransfunction available in the wctype library. The towctrans function is used to convert the given character using a transformation.

Example

The snippet below shows how the wctrans function converts characters from lower case to upper case.

The “toupper” is passed as an argument to the wctrans function, which returns a type wctrans_t value. The return value is passed as the second argument to the towctrans function, while the character that needs to be converted is passed as its first parameter.

Each character in the wide character string is accessed using a while loop. As the character is transformed, it is displayed on the console using the putwcharfunction, which takes as input only the character to be displayed. We can see this here:

#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
int main ()
{
wchar_t wide_char;
wchar_t my_string[] = L"welcome to EDUCATIVE!";
wctrans_t convert = wctrans("toupper");
int x = 0;
while ( my_string[x] )
{
my_string[x] = towctrans(my_string[x],convert);
putwchar (my_string[x]);
x = x+1;
}
return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring