strchr ()- 搜索字符

格式

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

语言级别

ANSI

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

strchr() 函数查找字符串中首次出现的字符。 字符 c 可以是空字符 (\0); 搜索中包含 string 的结束空字符。

strchr() 函数对以 null 结束的字符串进行操作。 函数的字符串自变量应包含标记字符串结束的空字符 (\0)。

返回值

strchr() 函数返回一个指向第一次出现的 c 的指针,该指针将转换为 string中的字符。 如果找不到指定的字符,那么此函数将返回 NULL

示例

此示例查找 计算机程序中首次出现的字符 p
#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char buffer1[SIZE] = "computer program";
  char * ptr;
  int    ch = 'p';
 
  ptr = strchr( buffer1, ch );
  printf( "The first occurrence of %c in '%s' is '%s'\n",
            ch, buffer1, ptr );
 
}
 
/*****************  Output should be similar to:  *****************
 
The first occurrence of p in 'computer program' is 'puter program'
*/

相关信息