shaobin0604
10/30/2009 - 7:14 AM

tcpl_ex-2-5.c

/*
 * Exercise 2-5. 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.) 
 */
#include <stdio.h>

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

int any(const 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])
				return i;
	return -1;
}

int main(void)
{
	const char s1[] = "abcd";
	const char s2[] = "cd";
	int r = 2;

	if (r == any(s1, s2))
		printf("assert ok\n");
	return 0;
}