编写函数any(s1, s2)
,将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符则返回-1。(标准库函数strpbrk
具有同样的功能,但它返回的是指向该位置的指针)
从头对s1进行判断,如果其有一个在s2中则返回。
#include <stdio.h>
#include <string.h>
char* s_gets(char *p, int length);
int any(char s1[], char s2[]);
int main(void)
{
char array[10] = {'0'};
puts(s_gets(array, 10));
printf("Please enter the character that you want to delete:\n");
char array1[10] = {'0'};
puts(s_gets(array1, 10));
printf("%d\n",any(array, array1));
return 0;
}
int any(char s1[], 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;
}
char* s_gets(char *p, int length)
{
char *q;
char *find;
q = fgets(p, length, stdin);
if (q)
{
find = strchr(q, '\n');
if (find)
{
*find = '\0';
}
else
{
while (getchar() != '\n')
continue;
}
}
return q;
}
3在位置2。