/*
* strchr 查找字符在字符串第一次出现的位置
*/
#include <cstdio>
char * strchr ( const char * str, int character ) {
char *p = (char *)str;
while (*p != '\0') {
if (*p == character) return p;
++p;
}
return nullptr;
}
int main () {
char str[] = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL) {
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
return 0;
}
C 标准库 strchr 函数的实现
最新推荐文章于 2024-02-25 16:00:00 发布