X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fstrchr.c;h=5f8db573773410349c8fd16eb5b812254bef0eb6;hb=b1fc26afebd4d557ff89a44bc21767a8704c3809;hp=fb169049e22171e7f7fb9200c44fdf3ebbe653ef;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec;p=pdclib diff --git a/functions/string/strchr.c b/functions/string/strchr.c index fb16904..5f8db57 100644 --- a/functions/string/strchr.c +++ b/functions/string/strchr.c @@ -1,17 +1,38 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- +/* strchr( const char *, int ) -// ---------------------------------------------------------------------------- -// C++ + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ -const char * strchr( const char * s, int c ) { /* TODO */ }; -char * strchr( char * s, int c ) { /* TODO */ }; +#include -// ---------------------------------------------------------------------------- -// Standard C +#ifndef REGTEST -char * strchr( const char * s, int c ) { /* TODO */ }; +char * strchr( const char * s, int c ) +{ + do + { + if ( *s == (char) c ) + { + return (char *) s; + } + } while ( *s++ ); + return NULL; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + char abccd[] = "abccd"; + TESTCASE( strchr( abccd, 'x' ) == NULL ); + TESTCASE( strchr( abccd, 'a' ) == &abccd[0] ); + TESTCASE( strchr( abccd, 'd' ) == &abccd[4] ); + TESTCASE( strchr( abccd, '\0' ) == &abccd[5] ); + TESTCASE( strchr( abccd, 'c' ) == &abccd[2] ); + return TEST_RESULTS; +} +#endif