/* Returns the first location in the string s1 where any character * from the string s2 occurs, or -1 if s1 contains no characters from * s2. */ #define MAXLEN 50 /* maximum allowable input */ #include void getstring(char input[]); char any(char s1[], char s2[]); int main() { char s1[MAXLEN], s2[MAXLEN]; int foo; getstring(s1); getstring(s2); if ((foo = any(s1, s2)) != -1) { printf("The first character in s1 which also occurs in s2 is\n"); printf("%c, character number %d (zero based).\n", s1[foo], foo); } else printf("No matches between the two strings.\n"); return 0; } void getstring(char input[]) { char i, c; printf("Please enter a string: "); i = 0; while ((c = getchar()) != '\n') { if (c == EOF) { printf("Oh, I see! Running away, eh?\n"); exit(0); } else if (i == MAXLEN) { printf("Ow, OW, OUCH! IT'S TOO BIG FOR ME!\n"); exit(0); } else input[i++] = c; } input[i] = '\0'; } char 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; }