cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstring (string.h) : strpbrk
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstring (string.h)
functions:
· memchr
· memcmp
· memcpy
· memmove
· memset
· strcat
· strchr
· strcmp
· strcoll
· strcpy
· strcspn
· strerror
· strlen
· strncat
· strncmp
· strncpy
· strpbrk
· strrchr
· strspn
· strstr
· strtok
· strxfrm
macros:
· NULL
types:
· size_t

-

strpbrk function
const char * strpbrk ( const char * str1, const char * str2 );
      char * strpbrk (       char * str1, const char * str2 );
<cstring>

Locate character 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.

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 declared as:

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

instead of the two overloaded versions provided in C++.

Example

/* 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)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us