Created
October 30, 2009 07:14
-
-
Save shaobin0604/222196 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment