这个问题令我困惑的就是“任一字符”,难道还要随便挑一个么?为了简单,我只是遇到第一个符合条件的就返回了。
/*
练习2-5 编写函数any(s1,s2),它把字符串s2中任一字符在字符串s1中的第一次出现的位置
作为结果返回。如果s1中没有包含s2中的字符,那么返回-1。(标准库函数strpbrk具有同样
的功能,但它返回的是指向该位置的指针。)
*/
#include <stdio.h>
#define MAX 50
int any(char s1[], char s2[])
{
int i, j;
for (j = 0; s2[j] != '\0'; ++j)
for (i = 0; s1[i] != '\0'; ++i)
if (s1[i] == s2[j])
return i;
return -1;
}
main()
{
char s1[MAX], s2[MAX];
printf("input s1:");
scanf("%s", s1);
printf("input s2:");
scanf("%s", s2);
printf("any(s1, s2):%d\n", any(s1, s2));
}