function
<cstring>
strpbrk
const char * strpbrk ( const char * str1, const char * str2 );
char * strpbrk ( char * str1, const char * str2 );
Locate characters in string
Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.
The search does not include the terminating null-characters of either strings, but ends there.
Parameters
- str1
- C string to be scanned.
- str2
- C string containing the characters to match.
Return Value
A pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if none of the characters of str2 is found in str1 before the terminating null-character.
If none of the characters of str2 is present in str1, a null pointer is returned.
Portability
In C, this function is only declared as:
char * strpbrk ( const char *, const char * );
instead of the two overloaded versions provided in C++.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
/* strpbrk example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a sample string";
char key[] = "aeiou";
char * pch;
printf ("Vowels in '%s': ",str);
pch = strpbrk (str, key);
while (pch != NULL)
{
printf ("%c " , *pch);
pch = strpbrk (pch+1,key);
}
printf ("\n");
return 0;
}
| |
Output:
Vowels in 'This is a sample string': i i a a e i
|
See also
- strcspn
- Get span until character in string (function
)
- strchr
- Locate first occurrence of character in string (function
)
- strrchr
- Locate last occurrence of character in string (function
)
- memchr
- Locate character in block of memory (function
)