在C语言中,可以使用strchr函数来查找指定字符在字符串中的位置。
函数原型:
char *strchr(const char *s, int c);参数说明:
s:要查找的字符串c:要查找的字符返回值:
如果找到指定字符,则返回指向该字符的指针。如果未找到指定字符,则返回NULL。示例代码:
#include <stdio.h>#include <string.h>int main() { char str[] = "Hello, World!"; char *ptr = strchr(str, 'o'); if (ptr != NULL) { printf("找到字符'o'在字符串中的位置:%d\n", ptr - str); } else { printf("未找到指定字符\n"); } return 0;}输出结果:
找到字符'o'在字符串中的位置:4 

