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 wchart , 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 wchart datatype .
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 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:
// 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 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:
size_twcslen (constwchar_t* wcs);
An easy C++ implementation that demonstrates how to determine the length of a wide character array string is provided 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 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:
wchar_t *wcscpy(wchar_t *strDestination, constwchar_t *strSource);
Example:
Here is a straightforward C++ implementation that uses wcscpy:
// 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 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:
wchar_t *wcscat(wchar_t *strDestination, constwchar_t *strSource);
Example:
Here is a straightforward C++ implementation that uses wcscat:
// 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 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:
constwchart wcs1, constwchart wcs2, int wcscmp;
Example:
Here is a straightforward C++ implementation that uses 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:
Wide-Character String Tokenize is the function's official name.
Syntax:
wchar_t* wcstok( wchar_t* str, constwchar_t* delim, wchar_t ** ptr);
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:
// 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 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:
wchart wcsncpy(wchart destination, constwchart* source, sizet n);
Example:
Here is a stated C++ implementation that uses wcsncpy :
// 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 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 :
// 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