Syntax and Parameters of wcslen()
Syntax of wcslen()
size_t wcslen(const wchar_t* str);
The wcslen() function returns the length of null-terminated wide string str as an argument. The null wide character is not included in the length. The function's behaviour is undefined if there is no null wide character in the wide string.
wcslen() Parameters
str: Pointer to the null-terminated wide string with an unknown length.
This method has only one parameter, str, which is a pointer to a wide string whose length needs to be calculated.
Return Value: The length of a wide string is returned by this function.
Examples of wcslen()
Example 1:
The following program demonstrates the above function: -
Code:
// c++ program to demonstrate
// example of wcslen() function.
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.utf8");
// Get the string to be used
wchar_t str1[] = L"Happy Coding Ninjas\u0021";
wchar_t str2[] = L"Hello Ninjas World\u0021";
int len1 = wcslen(str1);
int len2 = wcslen(str2);
// Get size of the string using wcslen()
cout << "Length of str1 = " << len1 << endl;
cout << "Length of str2 = " << len2 << endl;
if (len1 > len2)
cout << "str1 is longer than str2";
else if (len1 < len2)
cout << "str2 is longer than str1";
else
cout << "str1 and str2 are of equal length";
return 0;
}
Output:
Length of str1 = 20
Length of str2 = 19
str1 is longer than str2
--------------------------------
Process exited after 0.08483 seconds with return value 0
Example 2:
// c++ program to demonstrate
// example of wcslen() function.
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.utf8");
// Get the string to be used
wchar_t s[] = L"A free computer science portal for Ninjas";
// Get size of the string using wcslen()
wcout << L"The length of '" << s
<< L"' is =" << wcslen(s) << endl;
return 0;
}
Output:
When you run the program, you will get the following results:
The length of 'A free computer science portal for Ninjas' is =41
Example 3:
// c++ program to demonstrate
// example of wcslen() function.
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.utf8");
// Get the string to be used
wchar_t s[] = L"CodingNinjas";
// Get size of the string using wcslen()
wcout << L"The length of '" << s
<< L"' is =" << wcslen(s) << endl;
return 0;
}
Output:
The length of 'CodingNinjas' is = 12
We saw that program returned the length of the string as 12 as output
You practice by yourself with the help of online c++ compiler.
Frequently Asked Questions
What exactly is Wcslen?
The wcslen() function is defined in the header file cwchar.h. The wcslen() function returns the length of a wide string that has been specified. The number of non-null wide characters preceding the concluding null wide-character determines the length of a wide string.
What is a wchar_t in C++?
The wchar_t type is a wide-character type specified by the implementation. It's a 16-bit wide character in the Microsoft compiler that's used to store Unicode encoded as UTF-16LE, Windows' native character type.
What is Strnlen in C?
The strlen() function returns the length of the string pointed to by s, excluding the null character at the end. The strnlen() function calculates the length of s, but it only scans the first maxlen bytes.
Conclusion
In this blog, we have extensively discussed the wcslen() function in C++. We hope that this article has provided you with additional information about the wide-character length function. And to learn in-depth about c++ functions, check out the course on our c++ function on the Coding Ninjas website. And also, check out the awesome content on the Coding Ninjas Website, Android Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, and Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.
Happy Coding!
Recommended Readings: