The strchr () function in C

Char * strchr function (const char * str, int c) looks for the first occurrence of the character c (an unsigned char) in the string pointed to by parameter str.

Declare the strchr () function in C

Below is the declaration for strchr () in C:

 char * strchr ( const char * str , int c ) 

Parameters

str - This is the string to be scanned.

c - This is the character to be searched in str.

Returns the value

This function returns a pointer to the first occurrence of the c character in the str string, or returns NULL if the character is not found.

For example

The following C program illustrates the usage of strchr () in C:

 #include #include int main () { const char str [] = "https://QTM.com/lap-trinh-c/index.jsp" ; const char ch = '.' ; char * ret ; ret = strchr ( str , ch ); printf ( "Chuoi dang sau dau |%c| la: n|%s|n" , ch , ret ); return ( 0 ); } 

Compiling and running the above C program will result:

The strchr () function in C Picture 1

According to Tutorialspoint

Previous article: Strncat () function in C

Next article: Function strcmp () in C

4 ★ | 1 Vote

May be interested

  • The function strcmp () in CPhoto of The function strcmp () in C
    the function int strcmp (const char * str1, const char * str2) compares the string pointed to by sr1 with the string pointed to by srt2.
  • The function strncmp () in CPhoto of The function strncmp () in C
    the function int strncmp (const char * str1, const char * str2, size_t n) compares the first n bytes of str1 and str2.
  • The strcoll () function in CPhoto of The strcoll () function in C
    the function int strcoll (const char * str1, const char * str2) compares strings str1 and str2. the result depends on setting lc_collate.
  • Strcpy () function in CPhoto of Strcpy () function in C
    char * strcpy function (char * dest, const char * src) copies the string pointed to by src to dest.
  • The strncpy () function in CPhoto of The strncpy () function in C
    strncpy char * (char * dest, const char * src, size_t n) copies up to n copies to n characters from the string pointed to by src to dest. in case the length of the src is smaller than n, the remainder or the remainder of dest will be filled with null values.
  • The strcspn () function in CPhoto of The strcspn () function in C
    the function size_t strcspn (const char * str1, const char * str2) calculates the length of the character segment of string str1 without containing the characters in str2.