You are reading the article Functions Of Wide Characters With Examples updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Functions Of Wide Characters With Examples
Definition of C++ wchar_tIn C++, wide characters are like character datatype except the fact that char data type takes space of one byte whereas wide-character takes space of two bytes. In some cases, the wide-character takes up four bytes of memory depending on the compiler. This can hold different 64K (65536) characters in those two bytes of space. That is, it can hold characters of UNICODE which an international standard that permits encoding characters any character in any language virtually. Let us see more details on wide characters in the below sections. In this article, we will discuss the functions and examples of C++ wchar_t.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Wide characters are written in the format as mentioned below.
wchar_tThis will be used in the programs for the implementation of wide characters.
Functions of Wide Characters Function: wcslen()Syntax:
wcslen ( const wchar_t* str ) ;Description: Function that helps in getting the wide-character string length.
Function: wcsncpy()Syntax:
wchar_t* wcsncpy( wchar_t* dst, const wchar_t* sr, size_t sn) ;Description: Function that helps in copying the sn characters from the source to destination. If the source end is smaller than the size sn, then the destination will have some null characters.
Function: wcscat()Syntax:
wchar_t* wcscat ( wchar_t* dst, const wchar_t* sr) ;Description: Function that helps in concatenating the source string to destination string.
Function: wcscpy()Syntax:
wchar_t* wcscpy ( wchar_t* dst, const wchar_t* sr) ;Description: Function that helps in copying the source string to destination string.
Function: wcscmp()Syntax:
wcscmp ( const wchar_t* str1, const wchar_t* str2) ;Description: Function that helps in comparing the first string and second string. This function is similar to normal string comparison.
Function: wcsstr()Syntax:
const wchar_t* wcsstr ( const wchar_t* str1, const wchar_t* str2) ;Description: Function that helps in finding the first appearance of the second string in the first string. Null will be returned if it is not present.
Function: wcstok()Syntax:
wchar_t* wcstok ( const wchar_t* str1, const wchar_t* delim , wchar_t ** ptr) ;Description: Function that helps in tokenizing the string that generated with the help of wide characters. A delimiter delim is also used for string tokenization.
Examples of C++ wchar_tLet us see some sample examples on wchar_t in this section.
Example #1CPP program to implement wide character and get the size of it
using namespace std; int main() { wchar_t c = L’S’ ; cout << “The wide character value ‘S’ is: ” << c << endl ; cout << “Wide character size is ” << sizeof(c) ; return 0; }
Output:
In this program, a wide character is declared first. On executing the code, the value and its size gets printed. Here, it can be seen that L is used as a prefix for wide-character literals as well as wide-character string literals that notifies the compiler that string or character is of type wide-char.
Example #2CPP program to implement wide character and get the size of it using wcslen()
Code:
using namespace std; int main() { wchar_t c[] = L”Hope never dies” ; cout <<“The wide character length of Hope never dies ” <<” is : ” << wcslen(c) << endl ; return 0 ; }
Output:
In this program, a wide character array string is declared first. On executing the code, the size of the string gets printed.
Example #3CPP program to copy a wide-character string to another location
Code:
using namespace std; int main() { wchar_t c[] = L”Hope never dies” ; wchar_t d[15] ; wcscpy(d, c); wcout << L”Original string is : ” << c << L”n Copied string is : ” << d << endl; return 0; }
Output:
Example #4CPP program to concatenate a wide-character string with another string
Code:
using namespace std; int main() { wchar_t c[] = L”Hope never dies” ; wchar_t d[] = L” and trust yourself” ; wcscat(c, d); wcout << L”Concatenated string is : ” << c << endl; return 0; }
Output:
In this program, two string arrays are declared first. On executing the code, it can be seen that both strings are concatenated using the function wcscat().
Example #5CPP program to compare a wide-character string with another string
Code:
using namespace std; int main() { wchar_t c[] = L”Hope never dies” ; wchar_t d[] = L” and trust yourself” ; wcout << L”Comparison of first string with second = ” << wcscmp(c, d) << endl; wcout << L”Comparison of first string with first string = ” << wcscmp(c, c) << endl; wcout << L”Comparison of second string with first string = ” << wcscmp(d, c) << endl; return 0; }
Output:
In this program also, two string arrays are declared first. Unlike the above program, this program is to compare two strings. On executing the code, it can be seen that 3 values are shown. When the first string is compared with the second string, 1 is returned as the value of a first string is higher than the second. In the second case, 0 is returned because the string is compared with itself. At last, in the third case, -1 is returned as the value of the first string is less than the second.
ConclusionIn this article, different aspects such as syntax, functions, and example of wchar_t C++ is explained in detail.
Recommended ArticlesThis is a guide to C++ wchar_t. Here we discuss the definition and Functions of wide Characters and examples of C++ wchar_t respectively. You may also have a look at the following articles to learn more –
You're reading Functions Of Wide Characters With Examples
Update the detailed information about Functions Of Wide Characters With Examples on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!