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

  1. /***
  2. *strstr.c - search for one string inside another
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines strstr() - search for one string inside another
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <string.h>
  13.  
  14. /***
  15. *char *strstr(string1, string2) - search for string2 in string1
  16. *
  17. *Purpose:
  18. *       finds the first occurrence of string2 in string1
  19. *
  20. *Entry:
  21. *       char *string1 - string to search in
  22. *       char *string2 - string to search for
  23. *
  24. *Exit:
  25. *       returns a pointer to the first occurrence of string2 in
  26. *       string1, or NULL if string2 does not occur in string1
  27. *
  28. *Uses:
  29. *
  30. *Exceptions:
  31. *
  32. *******************************************************************************/
  33.  
  34. char * __cdecl strstr (
  35.         const char * str1,
  36.         const char * str2
  37.         )
  38. {
  39.         char *cp = (char *) str1;
  40.         char *s1, *s2;
  41.  
  42.         if ( !*str2 )
  43.             return((char *)str1);
  44.  
  45.         while (*cp)
  46.         {
  47.                 s1 = cp;
  48.                 s2 = (char *) str2;
  49.  
  50.                 while ( *s1 && *s2 && !(*s1-*s2) )
  51.                         s1++, s2++;
  52.  
  53.                 if (!*s2)
  54.                         return(cp);
  55.  
  56.                 cp++;
  57.         }
  58.  
  59.         return(NULL);
  60.  
  61. }
  62.