home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strrchr.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  1KB  |  53 lines

  1. /***
  2. *strrchr.c - find last occurrence of character in string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines strrchr() - find the last occurrence of a given character
  8. *       in a string.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <string.h>
  14.  
  15. /***
  16. *char *strrchr(string, ch) - find last occurrence of ch in string
  17. *
  18. *Purpose:
  19. *       Finds the last occurrence of ch in string.  The terminating
  20. *       null character is used as part of the search.
  21. *
  22. *Entry:
  23. *       char *string - string to search in
  24. *       char ch - character to search for
  25. *
  26. *Exit:
  27. *       returns a pointer to the last occurrence of ch in the given
  28. *       string
  29. *       returns NULL if ch does not occurr in the string
  30. *
  31. *Exceptions:
  32. *
  33. *******************************************************************************/
  34.  
  35. char * __cdecl strrchr (
  36.         const char * string,
  37.         int ch
  38.         )
  39. {
  40.         char *start = (char *)string;
  41.  
  42.         while (*string++)                       /* find end of string */
  43.                 ;
  44.                                                 /* search towards front */
  45.         while (--string != start && *string != (char)ch)
  46.                 ;
  47.  
  48.         if (*string == (char)ch)                /* char found ? */
  49.                 return( (char *)string );
  50.  
  51.         return(NULL);
  52. }
  53.