C++ wcslen()

The wcslen() function in C++ returns the length of the given wide string.

The wcslen() function is defined in <cwchar> header file.

wcslen() prototype

size_t wcslen( const wchar_t* str );

The wcslen() takes a null terminated wide string str as its argument and returns its length. The length does not include the null wide character. If there is no null wide character in the wide string, the behaviour of the function is undefined.

wcslen() Parameters

  • str: Pointer to the null terminated wide string whose length is to be calculated.

wcslen() Return value

  • The wcslen() function returns the length of the null terminated wide string.

Example: How wcslen() function works?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
	setlocale(LC_ALL, "en_US.utf8");
	wchar_t str1[] = L"Hello World\u0021";
	wchar_t str2[] = L"\u0764\u077a\u077c\u079f\u07a1\u072e";
	int len1 = wcslen(str1);
	int len2 = wcslen(str2);
	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";
		cout << "str1 and str2 are of equal length";
	return 0;

When you run the program, the output will be:

Length of str1 = 12
Length of str2 = 6
str1 is longer than str2
Did you find this article helpful?