Get first occurence in a string from characters in a second string in C
K&R's Exercise 2-5 looked easy enough, so I gave it a shot and came up with this little beauty.
int any(const char str1[], const char str2[]) {
int i = 0;
while (str1[i] != '\0') {
int j = 0;
while (str2[j] != '\0') {
if (str1[i] == str2[j]) {
return i;
}
++j;
}
++i;
}
return -1;
}
Comments
No comments yet.
Leave a Comment
Note: Your comment may require approval before it is posted to the site.