strchr() — Search for Character
Format
#include <string.h>
char *strchr(const char *string, int c);
Language Level
ANSI
Threadsafe
Yes
Locale Sensitive
The behavior of this function might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.
Description
The strchr()
function
finds the first occurrence of a character in a string. The character c can
be the null character (\0); the ending null character of string is
included in the search.
The strchr()
function
operates on null-ended strings. The string arguments to the function
should contain a null character (\0) that marks the end of
the string.
Return Value
The strchr()
function
returns a pointer to the first occurrence of c that
is converted to a character in string. The
function returns NULL if the specified character is not found.
Example
This example finds the first occurrence
of the character
pin
computer program.
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char buffer1[SIZE] = "computer program";
char * ptr;
int ch = 'p';
ptr = strchr( buffer1, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n",
ch, buffer1, ptr );
}
/***************** Output should be similar to: *****************
The first occurrence of p in 'computer program' is 'puter program'
*/
Related Information
- strcat() — Concatenate Strings
- strcmp() — Compare Strings
- strcpy() — Copy Strings
- strcspn() — Find Offset of First Character Match
- strncmp() — Compare Strings
- strpbrk() — Find Characters in String
- strrchr() — Locate Last Occurrence of Character in String
- strspn() — Find Offset of First Non-matching Character
- wcschr() — Search for Wide Character
- wcsspn() — Find Offset of First Non-matching Wide Character
- <string.h>