Function name : strchr
Header file : <string.h>
Function prototype : char *strchr(const char *str, char c);
Function : Find the position of the first occurrence of the specified character in a string
Parameters : char *str is the target string to be found;
char c is the character to be found;
Return value : Returns the position where the character first appears if successful; returns NULL if failed;
Program example : Find the first occurrence of the specified character c in the string string
#include<string.h>#include<stdio.h>intmain(void){charstring[15];//Define character array char*ptr,c='c';strcpy(string,www.dotcpp.com); //Copy the string ptr=strchr(string,c);//Find the first position where the character appears if(ptr){printf(Thecharacter%cisatposition:%dn,c,ptr-string);}else{ printf(Thecharacterwasnotfoundn);}return0;}
Running results:
Thecharacteristic position:7