the following to pieces of code are functions i have attempted to write. its suppose to return the first location in s1 that any character in s2 occurs.
my question is why doesnt the first set of code work? i think its because of the 2nd for loop that has s2[j] != s1[i] but im not sure. the only differences between the first and a second sets of code is the extra variable and the "s2[j] !=s1[i]" part.
for those of you that have k&r this is ex. 2-5.
thanks in advance for response guys...and girls.
Code:
//non-working code
int any(char s1[], char s2[])
{
int i,j;
for(i = 0; s1[i] != '\0'; i++)
{
for(j = 0; s2[j] != '\0' && s2[j] != s1[i]; j++)
{
if (s1[i] == s2[j])
return i;
else
return -1;
}
}
}
Code:
int any(char s1[], char s2[])
{
//working code
int i;
int j;
int pos;
pos = -1;
for (i = 0; s1[i] != '\0' && pos == -1; i++)
{
for (j = 0; s2[j] != '\0' && pos == -1; j++)
{
if (s2[j] == s1[i])
{
pos = i;
return pos;
}
}
}
}
i