This function searches for the first occurrence of a character in a string.
Syntax:
strchr(str, c)
Parameters:
- str: A pointer to the string to search.
- c: The character to search for (it’s treated as an int but represents a character).
Return Value:
- A pointer to the first occurrence of the character c in str.
- if the character c is not found in the string then value NULL will be return.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "My World";
char *ptr = strchr(str, 'W');
if (ptr != NULL) {
printf("First occurrence of 'W': %s\n", ptr); // Output: World
} else {
printf("'W' not found.\n");
}
return 0;
}
Output:
First occurrence of ‘W’: World