编写函数any(s1,s2),将字符串s2中任一字符在s1中第一次出现的位置作为返回结果。如果s1中不包含s2中的字符,则返回-1 。
代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
int any(const char *s1, const char *s2);
int main(int argc, char *argv[])
{
char s1[MAXLINE+1] = "hello world! This is a c program !";
char s2[MAXLINE+1] = " hab";
printf("%d\n", any(s1, s2));
return 0;
}
int any(const char *s1, const char *s2)
{
for (int i = 0; s1[i] != '\0'; ++i) {
for (int j = 0; s2[j] != '\0'; ++j) {
if (s1[i] == s2[j]) return i;
}
}
return -1;
}