strpbrk Function

Search a string for the first occurrence of a character from a specified set of characters.

Include

<string.h>

Prototype

char *strpbrk(const char *s1, const char *s2);

Arguments

s1
pointer to the string to be searched
s2
pointer to characters to search for

Return Value

Returns a pointer to the matched character in s1 if found; otherwise, returns a null pointer.

Remarks

This function will search s1 for the first occurrence of a character contained in s2.

Example

#include <string.h>
#include <stdio.h>

int main(void)
{
  char str1[20] = "What time is it?";
  char str2[20] = "xyz";
  char str3[20] = "eou?";
  char *ptr;
  int res;

  printf("strpbrk(\"%s\", \"%s\")\n", str1, str2);
  ptr = strpbrk(str1, str2);
if (ptr != NULL)
  {
    res = ptr - str1 + 1;
    printf("match found at position %d\n", res);
  }
  else
    printf("match not found\n");
  printf("\n");

  printf("strpbrk(\"%s\", \"%s\")\n", str1, str3);
  ptr = strpbrk(str1, str3);
if (ptr != NULL)
  {
    res = ptr - str1 + 1;
    printf("match found at position %d\n", res);
  }
  else
    printf("match not found\n");
}

Example Output

strpbrk("What time is it?", "xyz")
match not found

strpbrk("What time is it?", "eou?")
match found at position 9