温馨提示×

c语言strchr函数的用法是什么

小亿
111
2023-08-04 23:13:48
栏目: 编程语言

c语言的strchr函数用于在字符串中查找指定字符的第一次出现位置。

函数原型:

char *strchr(const char *str, int c);

参数解释:

  • str: 要查找的字符串。

  • c: 要查找的字符,以int的形式传入。

返回值:

  • 如果找到指定字符,则返回该字符在字符串中的地址(指针)。

  • 如果未找到指定字符,则返回NULL。

使用示例:

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr;
ptr = strchr(str, 'o');
if (ptr != NULL) {
printf("指定字符第一次出现的位置是:%s\n", ptr);
} else {
printf("未找到指定字符。\n");
}
return 0;
}

输出结果:

指定字符第一次出现的位置是:o, World!

0