Blame


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