c语言函数字符查找问题

定义一个函数:int find(char *p,char ch),在p所指字符串中查找ch字符,如果存在,返回它第一次出现的位置(位置从0开始),否则返回-1。 主函数会自动完成输入、函数调用和输出。你只需要提交find函数部分的代码片段即可

img

img


C语言关于函数指针问题需要求解

供参考:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int find(char* p, char ch)
{
    char* s = p;
    while (*s)
    {
        if (*s == ch)
            return s - p;
        s++;
    }
    return -1;
}
int main()
{
    char str[100], ch[3];
    scanf("%s", str);
    scanf("%s", ch);
    printf("%d\n", find(str, ch[0]));
    return 0;
}