-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathany.c
56 lines (43 loc) · 1.1 KB
/
any.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <string.h>
/* return the first location in the string s1 where any character
* from teh string s2 occurs. */
#define NOT_FOUND -1
int any(char s1[], char s2[]);
int indexOf(char search_for, char search_in[]);
int main()
{
char matching_input[] = "kids";
char non_matching_input[] = "books";
char target_chars[] = "di";
int result;
result = any(matching_input, target_chars);
printf("Matching: %d\n", result);
result = any(non_matching_input, target_chars);
printf("Matching: %d\n", result);
}
int any(char s1[], char s2[])
{
int s2_len = strlen(s2);
int current_index;
int result = NOT_FOUND;
for(int i = 0; i < s2_len; i++) {
current_index = indexOf(s2[i], s1);
if (result == NOT_FOUND
|| (current_index != NOT_FOUND
&& current_index < result)) {
result = current_index;
}
}
return result;
}
int indexOf(char search_for, char search_in[])
{
int search_in_len = strlen(search_in);
for(int i = 0; i < search_in_len; i++) {
if (search_for == search_in[i]) {
return i;
}
}
return NOT_FOUND;
}