For every character of s1, we need to compare it with every
character of s2. This seems to call for a pair of nested
for
-loops! If at any point a character in
s1 equals a character in s2, we return the index
of the character in s1. However, if we reach the end of the
loop—which can only occur if we had not already returned from the
function—we return -1
.
/* any: return location of first occurence of any character from s2 in s1 */
int any(char s1[], char s2[])
{
int i, j;
for (i = 0; s1[i] != '\0'; ++i)
for (j = 0; s2[j] != '\0'; ++j)
if (s1[i] == s2[j])
return i;
return -1;
}