#include <string.h>
char *strchr( const char *str, int ch );
函数返回一个指向 str 中 ch 首次出现的位置,当没有在 str 中找 ch 到返回 NULL。
#include <string.h> #include <stdio.h> int main(void) { char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); 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 r is at position: 12