-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.10.5.c
52 lines (39 loc) · 823 Bytes
/
ex.10.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
#include <stdio.h>
int findString (char text[], char pattern[])
{
int i, j;
for ( i = 0; text[i] != '\0'; ++i ) {
j = 0;
while ( pattern[j] != '\0' && pattern[j] == text[i + j] ) {
++j;
}
if ( pattern[j] == '\0' ) {
return i;
}
}
return -1;
}
int main (void)
{
int index;
// expect 3
index = findString ("a chatterbox", "hat");
printf ("%i\n", index);
// expect -1
index = findString ("a", "aa");
printf ("%i\n", index);
// expect -1
index = findString ("a chatterbox", "hatx");
printf ("%i\n", index);
// expect -1
index = findString ("", "hat");
printf ("%i\n", index);
// expect 0
index = findString ("a chatterbox", "");
printf ("%i\n", index);
// expect 0
char a000[] = {'a', '\0', '\0', '\0' };
index = findString (a000, "a");
printf ("%i\n", index);
return 0;
}