-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-5.c
74 lines (56 loc) · 1.93 KB
/
2-5.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Write the function any (s 1, s2 ), which returns the first location
in the string s 1 where any character from the string s2 occurs, or -1 if s 1
contains no characters from s2. (The standard library function strpbrk does
the same job but returns a pointer to the location.
*/
#include <stdio.h>
#include <strings.h>
#define LIM 20
int get_line1(char a[], char b[]);
int c_any (char str1[], char str2[]);
int main(){
char ori_str[LIM];
char match_str[LIM];
get_line1(ori_str, match_str);
printf ("1st index of matched char on string 1 is : %d \n", c_any(ori_str, match_str));
return 0;
}
int c_any (char str1[], char str2[]){
int i, k, found;
found = 0; /* flag for string match found */
/* check each character on str1 until terminator */
for ( i = 0; str1[i] != '\0'; i++){
/* check each characotor on s2 until teminator */
for (k = 0; str2[k] != '\0'; k++){
/* if 1st matched - return i */
if (str1[i] == str2[k] && found == 0){
found = 1;
return i;
}
}
}
return -1;
}
/*
getLine: read a line return int - the length
(changed the name from getline as stdio.h has a declared func with the same name)
arg[0]: char array - input text
arg[1]: int - the maimum limit of the length of input text (arg [0])
*/
int get_line1(char a[], char b[]){
int c, i, k;
printf ("Please type the original \n");
for (i = 0; i < LIM - 1 && (c = getchar()) != EOF && c != '\n'; i++){
a[i] = c;
}
/* once excuted it will be added as ++i so no need to add extra */
a[i] = '\0';
printf("Original Str: %s \n", a);
printf ("Please type the removing chars \n");
for (i = 0; i < LIM - 1 && (k = getchar()) != EOF && k != '\n'; i++){
b[i] = k;
}
printf("Removing Str: %s \n", b);
return i;
}