Skip to content

Commit

Permalink
[c_book] ex-2-5-any
Browse files Browse the repository at this point in the history
  • Loading branch information
ooddaa committed May 21, 2024
1 parent d6f5879 commit c2c2712
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions c_book/2-ch/exercises/ex-2-5-any.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<stdio.h>
#define MAX 100

/*
* int any(char[] s1, char[] s2)
* returns first location in s1
* where any char of s2 is found
* retunrs -1 if nothing is found
* */

int any(char[], char[]);
int any(char s1[], char s2[])
{
int i = 0, k, loc = -1;

while (loc < 0 && s1[i] != '\0') {
for (k = 0; s2[k] != '\0'; ++k) {
if (s1[i] == s2[k]) loc = i;
}
++i;
}

return loc;
}

int main(void)
{

printf("%d\n", any("lol", "abc")); // -1
printf("%d\n", any("lol", "al")); // 0
printf("%d\n", any("ol", "al")); // 1
printf("%d\n", any("abcde", "xyze")); // 4

return 0;
}

0 comments on commit c2c2712

Please sign in to comment.