home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / tinymud2.zip / STRINGUT.C < prev    next >
C/C++ Source or Header  |  1990-09-02  |  846b  |  38 lines

  1. #include "copyright.h"
  2.  
  3. /* String utilities */
  4.  
  5. #include <ctype.h>
  6.  
  7. #define DOWNCASE(x) (isupper(x) ? tolower(x) : (x))
  8.  
  9. int string_compare(const char *s1, const char *s2)
  10. {
  11.     while(*s1 && *s2 && DOWNCASE(*s1) == DOWNCASE(*s2)) s1++, s2++;
  12.  
  13.     return(DOWNCASE(*s1) - DOWNCASE(*s2));
  14. }
  15.  
  16. int string_prefix(const char *string, const char *prefix)
  17. {
  18.     while(*string && *prefix && DOWNCASE(*string) == DOWNCASE(*prefix))
  19.     string++, prefix++;
  20.     return *prefix == '\0';
  21. }
  22.  
  23. /* accepts only nonempty matches starting at the beginning of a word */
  24. const char *string_match(const char *src, const char *sub)
  25. {
  26.     if(*sub != '\0') {
  27.     while(*src) {
  28.         if(string_prefix(src, sub)) return src;
  29.         /* else scan to beginning of next word */
  30.         while(*src && isalnum(*src)) src++;
  31.         while(*src && !isalnum(*src)) src++;
  32.     }
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
  38.