c语言的strchr函数用于在字符串中查找指定字符的第一次出现位置。
函数原型:
char *strchr(const char *str, int c);
参数解释:
-
str: 要查找的字符串。
-
c: 要查找的字符,以int的形式传入。
返回值:
-
如果找到指定字符,则返回该字符在字符串中的地址(指针)。
-
如果未找到指定字符,则返回NULL。
使用示例:
#include#include 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!