]> pd.if.org Git - pdclib/blob - functions/string/strstr.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / string / strstr.c
1 /* strstr( const char *, const char * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <string.h>
8
9 #ifndef REGTEST
10
11 char * strstr( const char * s1, const char * s2 )
12 {
13     const char * p1 = s1;
14     const char * p2;
15     while ( *s1 )
16     {
17         p2 = s2;
18         while ( *p2 && ( *p1 == *p2 ) )
19         {
20             ++p1;
21             ++p2;
22         }
23         if ( ! *p2 )
24         {
25             return (char *) s1;
26         }
27         ++s1;
28         p1 = s1;
29     }
30     return NULL;
31 }
32
33 #endif
34
35 #ifdef TEST
36 #include "_PDCLIB_test.h"
37
38 int main( void )
39 {
40     char s[] = "abcabcabcdabcde";
41     TESTCASE( strstr( s, "x" ) == NULL );
42     TESTCASE( strstr( s, "xyz" ) == NULL );
43     TESTCASE( strstr( s, "a" ) == &s[0] );
44     TESTCASE( strstr( s, "abc" ) == &s[0] );
45     TESTCASE( strstr( s, "abcd" ) == &s[6] );
46     TESTCASE( strstr( s, "abcde" ) == &s[10] );
47     return TEST_RESULTS;
48 }
49 #endif