这个是问题
下面示例代码实现了函数 match 的功能,望采纳
#include <stdio.h>
char * match(char *s, char ch)
{
char *p = NULL;
while (*s != '\0')
{
if (*s == ch)
{
p = s;
}
s++;
}
return p;
}
在这个示例中,函数 match 接受两个参数:字符串首地址 s 和要查找的字符 ch。然后使用指针遍历字符串,如果找到了字符 ch,就更新指针 p 的值;如果没有找到,则指针 p 的值为空指针 NULL。最后,返回指针 p 的值。
你可以直接用数组的下表来实现,不需要用指针
供参考,谢谢!
#include <stdio.h>
char *match(char *s, char ch)
{
char *p = s;
while (*p++ != '\0')
;
while (p-- > s)
{
if (*p == ch)
return p;
}
return NULL;
}
int main(int argc, char *argv[])
{
char s[128], ch;
scanf("%[^\n]s", s);
scanf(" %c", &ch);
char *p = match(s, ch);
if (p)
puts(p);
else
puts("Not Found!");
return 0;
}