strchr函数的作用是寻找字符在字符串中出现的位置。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
const char * Strchr(const char * str, int c)
{
assert("str!=NULL");
while (*str != NULL)
{
if (*str == c)
{
return str;
}
str++;
}
return NULL;
}
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');
}
system("pause");
return 0;
}