Blob


1 /*
2 * 2-5. Write the function any(s1,s2), which returns the first location in
3 * the string s1 where any character from the string s2 occurs, or -1 if s1
4 * contains no characters from s2. (The standard library function strpbrk
5 * does the same job but returns a pointer to the location.)
6 */
7 #include <stdio.h>
9 int any(char s1[], char s2[]);
10 int strchr(const char *s, int c);
12 main () {
13 printf("2: %d\n", any("The quick brown fox jumped over the lazy dog.", "aeiou"));
14 printf("5: %d\n", any("The quick brown fox jumped over the lazy dog.", "aiou"));
15 printf("6: %d\n", any("The quick brown fox jumped over the lazy dog.", "aio"));
16 printf("12: %d\n", any("The quick brown fox jumped over the lazy dog.", "ao"));
17 }
19 int any(char s1[], char s2[]) {
20 int ret;
21 for (int i = 0; s1[i] != '\0'; ++i)
22 if (strchr(s2,s1[i]))
23 return i;
24 return -1;
25 }
26 int strchr(const char *s, int c) {
27 int i;
28 for (i = 0; *(s+i) != c; ++i)
29 if (*(s+i) == '\0')
30 return 0;
31 return i;
32 }