strchr()

Published Aug 25, 2021Updated Dec 20, 2021
Contribute to Docs

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

All contributors

Looking to contribute?

Learn C on Codecademy