C语言库函数strchr

原型

#include<string.h>  
char *strchr(char *str, int c);

功能

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

参数

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

返回值

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

示例

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

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");
    }
}
posted @ 2022-04-17 15:24  Galaxy_hao  阅读(220)  评论(0编辑  收藏  举报