home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / STRTOK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.5 KB  |  82 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strtok.c
  3.  *
  4.  * function(s)
  5.  *        strtok - searches one string for tokens, which are
  6.  *                 separated by delimiters defined in a second string
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19. #include <string.h>
  20.  
  21. static    char *Ss;
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            strtok - searches one string for tokens, which are
  26.                          separated by delimiters defined in a second string
  27.  
  28. Usage           char *strtok(char *str1, const char *str2);
  29.  
  30. Prototype in    string.h
  31.  
  32. Description    strtok considers the string str1 to consist of a sequence of
  33.         zero or more text tokens, separated by spans of one or more
  34.         characters from the separator string str2.
  35.  
  36.         The first call to strtok returns a pointer to the first
  37.         character of the first token in str1 and writes a null character
  38.         into str1 immediately following the returned token. Subsequent
  39.         calls with NULL for the first argument will work through the
  40.         string str1 in this way until no tokens remain.
  41.  
  42.         The separator string, str2, may be different from call to
  43.         call.
  44.  
  45. Return value    pointer to the scanned token.  When no tokens remain in str1,
  46.         strtok returns a NULL pointer.
  47.  
  48. *---------------------------------------------------------------------*/
  49. char *strtok(char *s1, const char *s2)
  50. {
  51.     register const char *sp;
  52.     char *tok;
  53.  
  54.     if (s1) Ss = (char *)s1;
  55.  
  56.     /* First skip separators */
  57.  
  58.     while (*Ss)
  59.     {
  60.         for (sp = s2; *sp; sp++)
  61.             if (*sp == *Ss)
  62.                 break;
  63.         if (*sp == 0)
  64.             break;
  65.         Ss++;
  66.     }
  67.     if (*Ss == 0)
  68.         return (0);
  69.     tok = Ss;
  70.     while (*Ss)
  71.     {
  72.         for (sp = s2; *sp; sp++)
  73.             if (*sp == *Ss)
  74.             {
  75.                 *Ss++ = 0;
  76.                 return (tok);
  77.             }
  78.         Ss++;
  79.     }
  80.     return (tok);
  81. }
  82.