C Library Function - strpbrk()
Description
C Library Function **char *strpbrk(const char *str1, const char *str2) ** searches the string str1 for the first character that matches a character in the string str2 characters, not including the null terminator. That is, the characters in the string str1 are checked one by one; when the checked character is also contained in the string str2, the check stops and the position of that character is returned.
Declaration
Below is the declaration of the strpbrk() function.
char *strpbrk(const char *str1, const char *str2)
Parameters
- str1 -- the C string to be searched.
- str2 -- this string contains the list of characters to match in str1.
Return Value
This function returns the number of the first character in str1 that matches a character in the string str2; if no character is found, it returns NULL.
Example
The following example demonstrates the use of the strpbrk() function.
Example
#include
<
stdio.h
#include
<
string.h
int
main
(
)
{
const
char
str1
[
]
=
"
abcde2fghi3jk4l
";
const
char
str2
[
]
=
"
34
"
;
char
ret
;
ret
=
strpbrk
(
str1
,
str2
)
;
if
(
ret
)
{
printf
(
"
The first matching character is: %c
\
n
"
, *
ret
)
;
}
else
{
printf
(
"
Character not found
"
)
;
}
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
第一个匹配的字符是: 3
C Standard Library -