strchr()

The strchr() string function finds the first occurrence of a given character.

Syntax

The string.h header file needs to be included at the top of the file:

#include <string.h>

strchr() function searches string for the character 'c':

strchr(string, 'c')

Example

Using strchr() function to find a specific character in the alphabet:

#include <stdio.h>
#include <string.h>
int main() {
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
printf("%s", strchr(alphabet, 's'));
return 0;
}

The output would be:

stuvwxyz

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C on Codecademy

Contributors