Introduction:
Wide character is similar to the character data type, with the key distinction being that wide character requires double the space, allowing for the representation of much larger values. While the char data type is limited to 256 values following the ASCII table, wide char can handle up to 65536 values, specifically Unicode values. Unicode, a modern global standard, facilitates the encoding of characters spanning numerous languages and commonly used symbols.
The wchart type represents wide characters, similar to how char represents character constants. The size of this data type can vary, being either 2 or 4 bytes depending on the compiler. An example of an international language that utilizes the wchart datatype is Japanese.
An example of using wchar_t in plain C++ is shown below:
// a C++ example program showing how to utilize wchar_t
#include <iostream>
using namespace std;
int main()
{
wchar_t w = L'C';
cout<< "Wide character value:: " << w <<endl ;
cout<< "Size of the wide char is:: " <<sizeof(w);
return 0;
}
Output:
Wide character value:: 67
Size of the wide char is:: 4
Explanation:
Wide-character literals and wide-character string literals are denoted by the prefix L, indicating to the compiler that the character or string belongs to the wide-char type.
When working with wide-char type, the prefix w is added to operations such as reading (wcin) and displaying (wcout).
Array or string of wide char type:
Wide-character type array strings are also achievable, similar to character type array strings. Below is an illustration of a wide-character type array string in C++:
Example:
// A C++ example program showing how to use wchar_t in an array
#include <iostream>
using namespace std;
int main()
{
// array string of char type
char caname[] = "JavaCppTutorial" ;
cout<<caname<<endl ;
// array string of wide-char type
wchar_twaname[] = L"JavaCppTutorial" ;
wcout<<waname<<endl;
return 0;
}
Output:
JavaCppTutorial
JavaCppTutorial
The wide character array requires double the memory to store each character's encoding, yet the resulting output remains unchanged.
Functions for wide character array strings:
The header file cwchar includes definitions for most of the functions related to wide character array strings.
wcslen:
It returns the length of the wide string. It is analogous to the strlen function for wide characters.
Syntax:
size_twcslen (constwchar_t* wcs);
A simple C++ implementation showcasing how to calculate the size of a wide character array string is presented below.
// A C++ program example showing how to utilise the wcslen() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
// wide-char type array string
wchar_twaname[] = L"JavaCppTutorial" ;
wcout<<L"The length of '" <<waname
<< L"' is " <<wcslen(waname) <<endl;
return 0;
}
Output:
The length of 'JavaCppTutorial' is 10
wcscpy:
Wide-character string copying is commonly known as wcscpy. The wide-character array pointed to by strDestination is populated with a wide-character string pointed to by strSource. This function serves as the wide character counterpart to strcpy.
Syntax:
wchar_t *wcscpy(wchar_t *strDestination, constwchar_t *strSource);
Example:
Here is a simple C++ implementation that utilizes the wcscpy function:
// A C++ program example showing how to utilise the wcscpy() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_twaname[] = L"javacpptutorial" ;
wchar_twacopy[14];
wcscpy(wacopy, waname);
wcout<<L"Original = " <<waname
<< L"\nCopy = " <<wacopy<<endl;
return 0;
}
Output:
Original = javacpptutorial
Copy = javacpptutorial
wcscat:
Wide character string concatenation is known as wcscat. This function appends a copy of the wide string stored in strSource to the wide string located at strDestination. Essentially, wcscat serves as the wide character version of strcat.
Syntax:
wchar_t *wcscat(wchar_t *strDestination, constwchar_t *strSource);
Example:
Here is a simple C++ code example that makes use of the wcscat function:
// A C++ program example showing how to utilise the wcscat() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_t string1[] = L"JavaCppTutorial" ;
wchar_t string2[] = L" is for JTP" ;
wcscat(string1, string2);
wcout<<L"Concatenated wide string is = "
<< string1 <<endl;
return 0;
}
Output:
Concatenated wide string is = JavaCppTutorial is for JTP
wcscmp:
This function is known as Wide-Character String Comparison. When wcs1 and wcs2 are identical, it will yield a result of 0. However, if the initial wide character that differs holds a higher value in wcs1 compared to wcs2, the function will return a value above zero. Similarly, if the first differing wide character has a lower value in wcs1 than in wcs2, it will output a value below zero. This function serves as the wide character equivalent to Strcmp.
Syntax:
Declare two constant wide character strings, wcs1 and wcs2, and an integer variable wcscmp.
Example:
Here is a simple C++ code implementation that employs wcscmp:
// A C++ program example showing how to utilise the wcscmp() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_t string1[] = L"JavaCppTutorial" ;
wchar_t string2[] = L"JTP" ;
wcout<< L"Compare1 = "
<<wcscmp(string1, string2) <<endl;
wcout<< L"Compare2 = "
<<wcscmp(string1, string1) <<endl;
wcout<< L"Compare3 = "
<<wcscmp(string2, string1) <<endl;
return 0;
}
Output:
Compare1 = 1
Compare2 = 0
Compare3 = -1
wcstok:
The function is officially named Wide-Character String Tokenize.
Syntax:
wchar_t* wcstok( wchar_t* str, constwchar_t* delim, wchar_t ** ptr);
Identifies the subsequent token in a wide string terminated by a null character, referenced by str. The delimiter characters in a wide string terminated by a null character, pointed to by delim, are used to determine separators. wcstok utilizes the ptr pointer to maintain its internal status in a wchar_t* object. Contrary to strtok, wcstok operates with wide characters.
Example:
Demonstrating the application of wcstok is depicted in the following C++ code implementation:
// A C++ program example showing how to utilise the wcstok() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_t string[] = L"JavaCppTutorial,is,for,JTP" ;
wchar_t* internal_state;
wchar_tdelim[] = L"," ;
wchar_t* token = wcstok(string, delim, &internal_state);
while (token)
{
wcout<< token <<endl;
token = wcstok(NULL, delim, &internal_state);
}
return 0;
}
Output:
JavaCppTutorial
is
for
JTP
wcsncpy:
It duplicates the initial n wide characters from the source to the target. If the end of the source wide string is reached before duplicating n characters, the destination is filled with additional null wide characters to reach a total of n characters. This function behaves similarly to the wide character version of strncpy.
Syntax:
Copy at most n characters from the source wide string to the destination wide string.
Example:
Here is a provided C++ implementation that utilizes the wcsncpy function:
// A C++ programme example showing how to utilise the wcsncpy() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_t string1[] = L"Java Cpp Tutorial";
wchar_t string2[20];
wchar_t string3[20];
wcsncpy(string2, string1, 20);
wcsncpy(string3, string2, 5);
string3[5] = L'\0';
wcout<< string1 <<endl<< string2
<<endl<< string3 ;
return 0;
}
O
Output:
Java Cpp Tutorial
Java Cpp Tutorial
Java ;???????
wcsstr:
Syntax:
constwchar_t* wcsstr (constwchar_t* wcs1, constwchar_t* wcs2);
It returns a pointer to the initial occurrence of wcs2 within wcs1. If wcs2 is not contained in wcs1, it returns a null pointer. In this context, wcs1 represents the wide character string to be searched, while wcs2 represents the substring to be located. This function is analogous to the wide character version of strstr.
Example:
A simple C++ implementation is presented here to illustrate the utilization of the wcsstr function:
// A C++ program example showing how to utilise the wcsstr() function
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
wchar_t string1[] = L"Java F point";
wchar_t* string2 = wcsstr(string1, L"F");
wcsncpy(string2, L"T", 1);
wcout<< string1 <<endl;
return 0;
}
Output:
Java T point