Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Standard library Header <cwchar>
3.
Syntax and Parameters of wcslen()
3.1.
Syntax of wcslen()
3.2.
wcslen() Parameters 
4.
Examples of wcslen() 
4.1.
Example 1:
4.2.
Example 2:
4.3.
Example 3:
5.
Frequently Asked Questions
5.1.
What exactly is Wcslen?
5.2.
What is a wchar_t in C++?
5.3.
What is Strnlen in C?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

wcslen() function in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In this blog, we will look at the wcslen() function in C++. In C++, the wcslen() method returns the length of a wide string.

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.

Also see, Literals in C, Fibonacci Series in C++

Standard library Header <cwchar>

This header was previously known as <wchar.h> in the C standard library.

This header is included in the libraries of null-terminated wide and multibyte strings. It also includes some C-style I/O functions and C-style Date conversion.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.hThe 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 DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview Experiences, and Coding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.
Happy Coding!

Recommended Readings:

Live masterclass