Strchar c: The function char *strchr(const char *str, int c); searches for the first occurrence of character c in the string pointed by str.
Function prototype of strchr
char *strchr(const char *str, int c);
- str : This is the string to be scanned for c.
- c : Character to be searched.
Return value of strchr
strchr in c: This function returns a pointer to the first occurrence of the character c in the string str. If character c is not found in str, it returns NULL.
C program using strchr function
The following program shows the use of strchr function to search a character in a string.
#include <stdio.h> #include <string.h> #include <conio.h> int main() { char string[100]; char ch, *ptr; printf("Enter any string\n"); gets(string); printf("Enter any character to search\n"); scanf("%c", &ch); ptr = strchr(string, ch); if(NULL == ptr){ printf("'%c' not found in string \"%s\"\n", ch, string); } else { printf("String after %c is \"%s\"\n", ch, ptr); } getch(); return(0); }
Output
TechCrashCourse Enter any character to search a String after a is "ashCourse"
Enter any string TechCrashCourse Enter any character to search z 'z' not found in string "TechCrashCourse"