Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strchr - First occurrence of a character in a string

&pagelevel(4)&pagelevel

Definition   

#include <string.h>

char *strchr(const char *s, int c);

strchr searches for the first occurrence of character c in string s and returns a pointer to the located position in s, if successful.

The terminating null byte (\0) is not counted as a character.

Return val.

Pointer to the position of c in string s


if successful.

NULL pointer

if c is not contained in string s.

Notes

The strchr and index functions are equivalent.

The following two prototypes of the strchr function are applicable to C++:
const char *strchr(const char *s, int c);

char *strchr( char *s, int c);

Example

Find the first ’s’:

#include <string.h>
#include <stdio.h>
int main(void)
{
   char *s = "What fun in the ssun!";
   printf("%s\n", s);
   printf("Where is the mistake? %s\n", strchr(s, 's'));
   return 0;
}

See also

index, rindex, strrchr