Javatpoint Logo
Javatpoint Logo

Wide Character in C++

Introduction:

Wide char is comparable to the char data type, however, wide char takes up twice as much space and can therefore accommodate significantly larger values. The 256 possible values for char correspond to the ASCII table's entries. Contrarily, wide char can accept up to 65536 values, which are UNICODE values. UNICODE is a relatively new worldwide standard that enables the encoding of characters for almost all languages and widely used symbols.

The type for wide characters is wchar_t, just like char is the type for character constants. Depending on the compiler being used, this data type can be 2 or 4 bytes in size. Japanese is a common example of an international language that uses the wchar_t datatype.

An example of using wchar_t in plain C++ is shown below:

Output:

Wide character value:: 67
Size of the wide char is:: 4

Explanation:

Wide-character literals and wide-character string literals are prefixed with the letter L, which informs the compiler that the char or string is of the wide-char type.

When using wide-char type, w is prefixed in operations like scanning (wcin) and printing (wcout).

Array or string of wide char type:

Wide-char type array strings are also possible, just like char type array strings. Here is an example of a wide-char type array string in C++:

Example:

Output:

JavaTpoint
JavaTpoint

The wide character array needs twice as much memory to encode each character, but the output remains the same.

Functions for wide character array strings:

The header file cwchar contains definitions for the majority of the functions for wide character array strings.

wcslen():

It gives back the wide string's length. It is equivalent Strlen's broad character.

Syntax:

An easy C++ implementation that demonstrates how to determine the length of a wide character array string is provided below.

Output:

The length of 'JavaTpoint' is 10

wcscpy():

Wide-Character String Copy is referred to as wcscpy(). The wide-character array addressed by strDestination receives a wide-character string that is pointed to by strSource. Strcpy's wide character equivalent is this.

Syntax:

Example:

Here is a straightforward C++ implementation that uses wcscpy:

Output:

Original = javatpoint
Copy = javatpoint

wcscat():

Wide-Character String Concatenation is referred to as wcscat(). It adds a duplicate of the wide string from strSource to the wide string at strDestination. It is strcat's wide character equivalent.

Syntax:

Example:

Here is a straightforward C++ implementation that uses wcscat:

Output:

Concatenated wide string is = JavaTpoint is for JTP

wcscmp():

This function is referred to Wide-Character String Comparison(). If wcs1 and wcs2 are equal, it returns 0, but if the first wide character that doesn't match has a higher value in wcs1 than in wcs2, it returns a value greater than zero. If the first wide character that doesn't match has a lower value in wcs1 than in wcs2, it also returns a value that is less than zero. It is the counterpart of Strcmp's broad character.

Syntax:

constwchar_t* wcs1, constwchar_t* wcs2, int wcscmp;

Example:

Here is a straightforward C++ implementation that uses wcscmp:

Output:

Compare1 = 1
Compare2 = 0
Compare3 = -1

wcstok():

Wide-Character String Tokenize() is the function's official name.

Syntax:

Identifies the next token within a broad string with a null termination that is pointed to by str. A broad string with a null termination that delim points to identify the separator characters. The wcstok uses the ptr pointer to store its internal state in an object of type wchar_t*. In comparison to strtok(), it uses wide characters.

Example:

The use of wcstok is illustrated in the stated C++ implementation below:

Output:

JavaTpoint
is
for
JTP

wcsncpy():

It copies the source's first n wide characters to the target. The destination is padded with extra null wide characters up to a total of n characters if the end of the source wide string is discovered before n characters have been duplicated. It is equivalent to strncpy()'s wide character.

Syntax:

wchar_t* wcsncpy(wchar_t* destination, constwchar_t* source, size_t n);

Example:

Here is a stated C++ implementation that uses wcsncpy:

Output:

Java T Point
Java T Point
Java ;???????

wcsstr():

Syntax:

It a pointer to the first instance of wcs2 in wcs1 is returned. If wcs2 is not a component of wcs1, it returns a null pointer. Here, the wide character string to be scanned is denoted by wcs1, and the sequence to match is found in wcs2. It is equivalent to strstr()'s wide character.

Example:

An easy C++ implementation is provided below to demonstrate how to use wcsstr:

Output:

Java T point






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA