home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRISTR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  94 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** Designation:  StriStr
  5. **
  6. ** Call syntax:  char *stristr(char *String, char *Pattern)
  7. **
  8. ** Description:  This function is an ANSI version of strstr() with
  9. **               case insensitivity.
  10. **
  11. ** Return item:  char *pointer if Pattern is found in String, else
  12. **               pointer to 0
  13. **
  14. ** Rev History:  07/04/95  Bob Stout  ANSI-fy
  15. **               02/03/94  Fred Cole  Original
  16. **
  17. ** Hereby donated to public domain.
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include "snip_str.h"
  24.  
  25. typedef unsigned int uint;
  26.  
  27. #if defined(__cplusplus) && __cplusplus
  28.  extern "C" {
  29. #endif
  30.  
  31. char *stristr(const char *String, const char *Pattern)
  32. {
  33.       char *pptr, *sptr, *start;
  34.       uint  slen, plen;
  35.  
  36.       for (start = (char *)String,
  37.            pptr  = (char *)Pattern,
  38.            slen  = strlen(String),
  39.            plen  = strlen(Pattern);
  40.  
  41.            /* while string length not shorter than pattern length */
  42.  
  43.            slen >= plen;
  44.  
  45.            start++, slen--)
  46.       {
  47.             /* find start of pattern in string */
  48.             while (toupper(*start) != toupper(*Pattern))
  49.             {
  50.                   start++;
  51.                   slen--;
  52.  
  53.                   /* if pattern longer than string */
  54.  
  55.                   if (slen < plen)
  56.                         return(NULL);
  57.             }
  58.  
  59.             sptr = start;
  60.             pptr = (char *)Pattern;
  61.  
  62.             while (toupper(*sptr) == toupper(*pptr))
  63.             {
  64.                   sptr++;
  65.                   pptr++;
  66.  
  67.                   /* if end of pattern then pattern was found */
  68.  
  69.                   if ('\0' == *pptr)
  70.                         return (start);
  71.             }
  72.       }
  73.       return(NULL);
  74. }
  75.  
  76. #if defined(__cplusplus) && __cplusplus
  77.  }
  78. #endif
  79.  
  80. #ifdef TEST
  81.  
  82. int main(void)
  83. {
  84.       char buffer[80] = "heLLo, HELLO, hello, hELLo, HellO";
  85.       char *sptr = buffer;
  86.  
  87.       while (0 != (sptr = stristr(sptr, "hello")))
  88.             printf("Found %5.5s!\n", sptr++);
  89.  
  90.       return(0);
  91. }
  92.  
  93. #endif /* TEST */
  94.