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

  1. /***
  2. *strcmp.c - routine to compare two strings (for equal, less, or greater)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compares two string, determining their lexical order.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <string.h>
  13.  
  14. #ifdef _MSC_VER
  15. #pragma function(strcmp)
  16. #endif  /* _MSC_VER */
  17.  
  18. /***
  19. *strcmp - compare two strings, returning less than, equal to, or greater than
  20. *
  21. *Purpose:
  22. *       STRCMP compares two strings and returns an integer
  23. *       to indicate whether the first is less than the second, the two are
  24. *       equal, or whether the first is greater than the second.
  25. *
  26. *       Comparison is done byte by byte on an UNSIGNED basis, which is to
  27. *       say that Null (0) is less than any other character (1-255).
  28. *
  29. *Entry:
  30. *       const char * src - string for left-hand side of comparison
  31. *       const char * dst - string for right-hand side of comparison
  32. *
  33. *Exit:
  34. *       returns -1 if src <  dst
  35. *       returns  0 if src == dst
  36. *       returns +1 if src >  dst
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. int __cdecl strcmp (
  43.         const char * src,
  44.         const char * dst
  45.         )
  46. {
  47.         int ret = 0 ;
  48.  
  49.         while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
  50.                 ++src, ++dst;
  51.  
  52.         if ( ret < 0 )
  53.                 ret = -1 ;
  54.         else if ( ret > 0 )
  55.                 ret = 1 ;
  56.  
  57.         return( ret );
  58. }
  59.