strchr()函数的详解与实现

一)strchr()函数的详解

原型:extern char *strchr(const char *s,char c);  

头文件:#include <string.h>   
功能:查找字符串s中首次出现字符c的位置

说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。

使用实例和实现算法:

#include <stdio.h>

char *strchr(const char *s, const char ch)
{
 	 if (NULL == s)
		 return NULL;	 
	 
	 const char *pSrc = s;
	 while ('\0' != *pSrc)
	 {
	  	   if (*pSrc == ch)
	  	   {
			   return (char *)pSrc;
		   }		   
	  	   ++ pSrc;
	 }	 
	 return NULL;
} 

void main()
{
	char *str = "0123456789";
	printf("%s\n",strchr(str,'t'));
}
说明:要注意该函数的返回值,以及形式参数的具体类型。

你可能感兴趣的:(c,String,null,character)