C++ wcspbrk()

The wcspbrk() function in C++ searches for a set of wide characters present in a wide string in another wide string.

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

wcspbrk() prototype

const wchar_t* wcspbrk( const wchar_t* dest, const wchar_t* str );
wchar_t* wcspbrk( wchar_t* dest, const wchar_t* str );

The wcspbrk() function takes two null terminated wide string: dest and src as its arguments.

It searches the null terminated wide string pointed to by dest for any wide character that is present in the wide string pointed to by src and returns the pointer to the first wide character in dest that is also found in src.


wcspbrk() Parameters

  • dest: Pointer to a null terminated wide string to be searched.
  • src: Pointer to a null terminated wide string containing the characters to search for.

wcspbrk() Return value

If the dest and src pointer has one or more wide characters in common, the wcspbrk() function returns the pointer to the first wide character in dest that is also in src.

If no wide characters in src is present in dest, a null pointer is returned.


Example: How wcspbrk() function works?

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

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t src[] = L"0123456789";
	wchar_t dest[] = L"\u0126\u014b\u01b8\u0246\u006a\u0039\u00b5\u04c5\u0927\u0032\u1264";
	wchar_t *s = wcspbrk(dest, src);
	int pos;
	
	if (s)
	{
		pos = s-dest;
		wcout << L"First occurrence of number in \"" << dest << L"\" is at position " << pos << endl;
	}
	else
		wcout << L"No number found in \"" << dest << "\"";
	
	return 0;
}

When you run the program, the output will be:

First occurrence of number in "ĦŋƸɆj9µӅध2ቤ" is at position 5