Wcsncpy Function In C++

The wcsncpy function is actually a C Standard Library function and is not a part of C++. The "wcs" prefix stands for "wide character string" , indicating that it is used for handling wide character strings, which are typically used in internationalization and Unicode support. Let's examine wcsncpy in greater detail:

Syntax:

It has the following syntax:

wchart wcsncpy(wchart destination, const wchart* source, sizet num);

Destination: A pointer to the destination array where the content will be copied.

Source: A pointer to the source of the data to be copied.

Num: The maximum number of wide characters to copy.

Behavior:

  • The wcsncpy function copies the string pointed to by source to the array pointed to by destination, copying at most num wide characters.
  • Null wide characters (L'\0') are added to the destination array until num characters are written in total if the length of the source string is less than num.
  • Wcsncpy will not null-terminate the destination string if the length of the source string is greater than or equal to num.

If num is greater than or equal to the length of the source string, wcsncpy does not guarantee the null-termination of the destination string. If you want null-termination, you may need to add it explicitly to the destination array after using wcsncpy.

Example 1:

Let's take an example to illustrate the use of the wcsncpy function in C++.

Example

#include <iostream>
#include <cwchar>
using namespace std;
int main() {
const wchar_t* source = L"Java Cpp Tutorial";
wchar_t destination[20];
wcsncpy(destination, source, 10);
destination[10] = L'\0';
wcout << destination <<endl;
return 0;
}

Output:

Explanation:

In this example, the destination array is explicitly null-terminated after the wcsncpy function copies a maximum of ten wide characters from the source string to the destination array. After that, std::wcout is used to print the outcome.

Example 2:

Let's take another example to illustrate the use of the wcsncpy function in C++.

Example

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
wchar_t src[] = L"Java Cpp Tutorial is my favorite website"; 
wchar_t dest[40]; 
wcsncpy(dest, src, 19); 
wcout << "Destination string is : " << dest; 
return 0; 
}

Output:

Example 3:

Let's take an example to illustrate the use of the wcsncpy function in C++.

Example

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
wchar_t src[] = L"Java Cpp Tutorial"; 
wchar_t dest[40]; 
wcsncpy(dest, src, 5); 
wcout << "Destination string is : " << dest; 
return 0; 
}

Output:

Example 4:

Let's take an example to illustrate the use of the wcsncpy function in C++.

Example

#include <wchar.h>
int main ()
{
wchar_t wcs1[] = L"I like java cpp tutorial";
wchar_t wcs2[40];
wchar_t wcs3[40];
wcsncpy ( wcs2, wcs1, 40 );
wcsncpy ( wcs3, wcs2, 6 );
wcs3[6] = L'\0'; 
wprintf (L"%ls\n%ls\n%ls\n",wcs1,wcs2,wcs3);
return 0;
}

Output:

Input Required

This code uses input(). Please provide values below: