Name
strpbrk
Synopsis
Finds the first character in a string that matches any character in another string
#include <string.h> char *strpbrk
( const char *s1
, const char *s2
);
The strpbrk()
function
returns a pointer to the first character in the string addressed by
s1
that matches any character contained
in the string addressed by s2
, or a null
pointer if the two strings have no characters in common.
Example
char *story = "He shouted: \"What? I can't hear you!\"\n";
char separators[ ] = " \t\n.:?!\"";
char *start = story, *end = NULL;
char words[16][16]; // An array of char arrays to collect words in.
int i = 0;
while ( i < 16 && ( end =strpbrk
( start, separators ) ) != NULL )
{
if ( end != start ) // If the separator wasn't the first character,
{ // then save a word in an array.
strncpy( words[i], start, end - start );
words[i][end - start] = '\0'; // And terminate it.
i++;
}
start = end + 1; // Next strpbrk call starts with
} // the character after this separator.
puts( story );
for ( int j = 0 ; j < i ; j++ )
puts( words[j] );
This program prints each of the words it has collected on a new line:
He shouted What I can't hear you
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.