In this guide, we will explore the wcspbrk function in C++, covering its syntax, parameters, and illustrations.
What is the wcspbrk function in C++?
The wcspbrk predefined function in C/C++ is a standard library function designed for detecting a group of wide characters within a different wide string through a search operation. This function is available in the <cwchar> header file.
This function requires two arguments: the first one represents the target location, while the second one represents the outcome. The destinations are considered as wide strings that are terminated by a null character. Wide strings are utilized to hold characters with a null terminator at the end. This function yields a pair of values.
Syntax:
It has the following syntax:
wcspbrk(dests, srcs)
Parameters:
Parameters in functions are typically specified in pairs, as demonstrated in the template below.
- dests: This parameter indicates the wide string pointed to by the pointer that will be searched, including the null terminator.
- srcs: This parameter refers to a pointer pointing to a wide character array containing all the characters to be searched for.
Return Value:
It includes a pair of values. These values are outlined below:
- When there are common characters in both the output and input, the function will provide the address of the initial wide character in the destination that matches a character in the source.
- In cases where no wide characters are found in both src and dest, only a null pointer will be assigned.
Example 1:
Let's consider a scenario to demonstrate the wcspbrk function in C++.
#include <cwchar>
#include <iostream>
using namespace std;
int main()
{
wchar_t srcStr[] = L"Allice John";
wchar_t destStr[] = L"Programming";
wchar_t* st = wcspbrk(destStr, srcStr);
int position;
if (st) {
position = st - destStr;
wcout << L"The First occurrence in the string: \"" << destStr
<< L"\" is found at the position " << position << endl;
}
else
wcout << L"The number has not found\"" << destStr << "\"";
return 0;
}
Output:
The First occurrence in the string: "Programming" is found at the position 2
Explanation:
- Two wide character arrays are declared: srcStr and destStr. These are the source and target strings. The L prefix in front of the string literals shows that they are wide-character strings.
- The variable st is a pointer to a wide character data type (wchar_t*). It will be saved as the position of the first character from srcStr in destStr.
- The wcspbrk function takes destStr and srcStr as its arguments. This function performs searching of the first location of any character from srcStr in destStr and returns the address to this character.
- If there is no character in the intersection, the pointer should be returned as null. If the wcspbrk determines that the hsearch in srcStr is complete and if it is found inside the destStr, the type of return is the NULL or non-null. It returns the index as the output.
- The output is shown through wcout, which is the wide-character equivalent of cout. It begins the description of the character with its name and position.
- The null pointer return value of wcspbrk gives rise to a conclusion that no characters from srcStr are in destStr. The program generates a message notifying that the person does not show up in this case.
Example 2:
Let's consider a scenario to demonstrate the wcspbrk function in C++.
#include <cwchar>
#include <iostream>
using namespace std;
int main()
{
wchar_t srcStr[] = L"529";
wchar_t destStr[] = L"Welcome";
wchar_t* st = wcspbrk(destStr, srcStr);
int position;
if (st) {
position = st - destStr;
wcout << L"The First occurrence in the string : \"" << destStr
<< L"\" is found at the position " << position << endl;
}
else
wcout << L"There is no common wide character";
return 0;
}
Output:
There is no common wide character
Explanation:
- This code gives an example of the wcspbrk function in C++. It finds the first match of one of the characters by checking it against srcStr inside destStr. The string will be searched for a character corresponding to the destStr string. After a match is found, its associated character position will be calculated and displayed. In case of no match takes place, the message gets displayed on the screen.
- The program will use not only the function <cwchar> for a universal character representation of strings but also the library <iostream> for input/output. This operation's result is returned by invoking wcspbrk(character, pointers); the position is then calculated via pointer operations.
Conclusion:
In summary, the C++ wcspbrk function operates on a two-dimensional array of characters, resulting in the character array provided being returned by the function. It produces a wide-character type that signifies the position of the initial occurrence of a character set within the wide character string. While similar to the C language strpbrk function in functionality, wcspbrk offers greater versatility as it operates with code points. This function requires two parameters: the target location should contain a wide string for the search operation, and the source area should be a widened string containing the characters to be searched.
The function provides a single-pointer sequence starting from the top and going down. In case there is no matching character, it points to a null row.
To employ the wcspbrk function, you need to include the header file <cwchar> to define the wcout stream, responsible for displaying wide characters. This stream is essential for identifying specific wide characters within the C++ code.