C语言库函数strchr


原型

#include  
char *strchr(char *str, int c);

功能

确定c(转换为char) 在str中第一次出现的位置,终止的空字符被认为是 字符串的一部分。因此,也可以定位它以检索指向字符串末尾的指针。

参数

  • str:要查找的字符串
  • c:要定位的字符,内部转为char

返回值

指向str中第一次出现的字符c的指针。如果未找到该字符,则返回一个空指针。

示例

#include 
#include 
#include 

int main()
{
    char *str = "hello,world\n";
    char c = 'e';
    char *ptr;

    ptr = strchr(str, c);
    if (ptr)
    {
        printf("find ptr:%s\n", ptr);
    }
    else
    {
        printf("not found\n");
    }
}