Latest web development tutorials

C library functions - strchr ()

C standard library - <string.h> C standard library - <string.h>

description

C library functionschar * strchr (const char * str , int c) in the parameter strpoints to the string to search for the first occurrence of the characterc (anunsigned character) position.

statement

Here is () statement strchr function.

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

parameter

  • str - the string to be retrieved C.
  • c - the character to be searched str.

return value

This function returns the position in string str the first occurrence of the character c, if the character is not found it returns NULL.

Examples

The following example demonstrates strchr () function is used.

#include <stdio.h>
#include <string.h>

int main ()
{
   const char str[] = "http://www.w3cschool.cc";
   const char ch = '.';
   char *ret;

   ret = strchr(str, ch);

   printf("|%c| 之后的字符串是 - |%s|\n", ch, ret);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

|.| 之后的字符串是 - |.w3cschool.cc|

C standard library - <string.h> C standard library - <string.h>