subject
Write the function any(s1, s2) to return the first occurrence of any character in the string S2 as a result. If s 1 does not contain the characters in s2, then - 1 is returned. (The standard library function strpbrk has the same function, but it returns a pointer to that location.)
Title Analysis
If you don't understand how any character works, mark the position of each character in string 1. The implementation method is similar to 2-4.
Programming implementation
#include <stdio.h>
#define MAXLINE 1000
int positionc(char s1[], char s2[], int p[]);
int main()
{
int i, positions[MAXLINE];
char c, sfirst[MAXLINE], ssecond[MAXLINE];
i = 0;
printf("Please input string1:");
while ((c = getchar()) != '\n')
sfirst[i++] = c;
sfirst[i] = '\0';
i = 0;
printf("Please input string2:");
while ((c = getchar()) != '\n')
ssecond[i++] = c;
ssecond[i] = '\0';
positionc(sfirst, ssecond, positions);
i = 0;
while (ssecond[i] != '\0')
{
printf("%c\t%d\n",ssecond[i], positions[i]);
i++;
}
}
int positionc(char s1[], char s2[], int p[])
{
int m, n, l, r;
n = 0;
while (s2[n] != '\0')
{
p[n] = 0;
m = l = 0;
while (s1[m] != '\0')
{
if (s1[m] != s2[n] && s1[m+1] != '\0')
l++;
else if ( s1[m] == s2[n])
{
p[n] = l;
break;
}
else if (s1[m] != s2[n] && s1[m + 1] == '\0')
{
p[n] = -1;
break;
}
m++;
}
if (s1[m] == '\0' && l == 0)
p[n] = -1;
n++;
}
}