There is a C standard library function that does almost all of this for us. This
removes two -Wconversion and one -Wsign-conversion warnings.
*/
int indexOf(char *s1, char *s2)
{
- char c1 = *s2;
- char c;
- char *p;
- int len2;
-
- if (c1 == '\0')
- return 0;
- p = s1;
- len2 = strlen(s2) - 1;
- while ((c = *p++)) {
- if (c != c1)
- continue;
- if (strncmp(p, s2 + 1, len2) == 0)
- return ((p - s1) - 1);
- }
- return -1;
+ char *s = strstr(s1, s2);
+ return s == NULL ? -1 : (int)(s1 - s);
}
/* rindexOf: