Dotcpp  >  编程教程  >  <string.h>头文件  >  C语言strchr()函数:查找字符串中第一个出现的指定字符的位置

C语言strchr()函数:查找字符串中第一个出现的指定字符的位置

点击打开在线编译器,边学边练

函数名:  strchr

头文件:<string.h>

函数原型:  char *strchr(const char *str, char c);

功能: 查找字符串中第一个出现的指定字符的位置

参数: char *str  为要查找的目标字符串;

           char c    为要查找的字符;  

返回值: 成功  返回字符第一次出现的位置;失败  返回NULL;


程序例:  查找字符串string中指定字符c的首次出现的位置

#include <string.h>

#include <stdio.h>

int main(void) {

   char string[15]; //定义字符数组

   char *ptr, c = 'c'; 

   strcpy(string, "www.dotcpp.com"); //复制字符串

   ptr = strchr(string, c); //查找字符出现的第一个位置

   if (ptr) {

      printf("The character %c is at position: %d\n", c, ptr-string);

   }else{

      printf("The character was not found\n");

   }

   return 0;

}

运行结果:

The character c is at position: 7



本文固定URL:https://www.dotcpp.com/course/582

C语言函数库
<assert.h>头文件
<ctype.h>头文件
<float.h>头文件
<io.h>头文件
<math.h>头文件
<mem.h>头文件
<setjmp.h>头文件
<stdio.h>头文件
<stdlib.h>头文件
<sigal.h>头文件
<string.h>头文件
<time.h>头文件
Dotcpp在线编译      (登录可减少运行等待时间)