The strchr function finds the first occurrence of a specified character in a string, If the specified character is found, the function returns a pointer to that character; otherwise, it returns a NULL pointer. Initially, the strchr function is used to locate the position of the first ‘ l ‘ character in strings and its address is assigned to pointer result.
#include <stdio.h>
char *strchr(char *string, char letter);
int main()
{
int i;
char strings[2][30]={"hello world","C programming"};
char letters[]={'l','z'},*result;
clrscr();
for (i=0;i<2;i++)
{
if(*(result=strchr(strings[i],letters[i]))!=NULL)
printf("\n\n\"%s\" was scanned for \'%c\'.\nFunction returned:-> %s",strings[i],letters[i],result);
else
printf("\n\n\'%c\' was not found in \"%s\"",letters[i],strings[i]);
}
getch();
return 0;
}
char *strchr(char *string, char letter)
{
char *ptr=NULL;
while (*string!=letter && *string!=NULL)
ptr=++string;
return (ptr);
}