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. - C String

C examples for String:String Function

Description

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.

Demo Code

#include <stdio.h>

#define MAXLINE 1000/*w  w w.j av a  2s  .  com*/

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

int main(void){
    int len;
    char s1[MAXLINE] = "this is a test";
    char s2[MAXLINE] = "test";

    printf("The first location is %d\n", any(s1, s2));

    return 0;
}
int any(char s1[], char s2[]){
    int i = 0, j;
    while (s2[i] != '\0') {
        j = 0;
        while (s1[j] != '\0') {
            if (s1[j] == s2[i])
                return j;
            ++j;
        }
        ++i;
    }

    return -1;
}

Result


Related Tutorials