编写一个名叫my_strnchr的函数,它的原型如下:
char *my_strnchr(char comst *str, int ch, int which);
这个函数类似于strchr,但它的第三个参数指定ch字符在str字符串中第几次出现,例如,第三个参数为1,这个函数的功能就和strchr一样,如果参数为2,这个函数就返回一个指向ch字符在str字符串中第二次出现的位置的指针。
char *my_strnchr(char const *str, int ch, int which) {
char *rs = NULL;
while(--which >= 0 && (rs = strchr(str, ch)) != NULL) {
str = rs + 1;
}
return rs;
}
int main()
{
char string[] = "192.168.0.199";
printf("%s\n", my_strnchr(string, ".", 3));
return 0;
}