Exercise 2.5 - return the first location in the string s1 comparing s2

Question

Write the function any(s1,s2), which returns the first location in a string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2. (The standard library function strpbrk does the same job but returns a pointer to the location.)

/**
 * Exercise 2.5
 *
 * Write the function any(s1,s2) which 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. ( The standard library function strpbrk does
 * the same job but returns a pointer to the location
 *
 **/

#include <stdio.h>

#define MAXLINE 1000

int mgetline(char line[], int lim);

int any(char s1[], const char s2[]);

int main(void) {
    char s1[MAXLINE], s2[MAXLINE];
    int val;

    mgetline(s1, MAXLINE);
    mgetline(s2, MAXLINE);

    val = any(s1, s2);

    printf("%d", val);

    return 0;
}

int mgetline(char line[], int lim) {
    int i, c;
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
        line[i] = c;

    if (c == '\n')
        line[i++] = c;
    line[i] = '\0';

    return i;
}

int any(char s1[], const char s2[]) {
    int i, j;

    for (i = 0; s1[i] != '\0'; ++i) {
        for (j = 0; s2[j] != '\0'; ++j) {
            if (s1[i] == s2[j] && s1[i] != '\n') {
                return i;
            }
        }
    }

    return -1;
}

Explanation

The important part of the program is the function any which takes two strings s1 and s2 and tries to find if any character in s2 matches s1. We set a flag, check_next_char which is toggled to 0 if we find the match, otherwise we have it as 1.

The first for loop iterates through all the characters in s1 while the condition check_next_char is 1. In the second for loop, if we find that a char in s2 matches s1, that is s2[j] == s1[i] and s2 has not reached EOL, then we set check_next_char to 0. That is we found a match at i and we return that.

If we dont find a match in s2, we increment i and take the next character from s1. If dont find a match at all, then we return -1.

Visualization

Try It Out