by Dinesh Thakur Category: C Programming (Pratical)

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;
            for (i=0;i<2;i++)
                       printf("\n\n\"%s\" was scanned for \'%c\'.\nFunction returned:->  %s",strings[i],letters[i],result);
                       printf("\n\n\'%c\' was not found in \"%s\"",letters[i],strings[i]);
                       return 0;
                       char *strchr(char *string, char letter)
                       char *ptr=NULL;
                       while (*string!=letter && *string!=NULL)
                                return (ptr);

Search For 1st Occurrence of Char in String

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.