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