home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / STRINGS.ZIP / ELT.C < prev    next >
Text File  |  1993-01-14  |  1KB  |  40 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4.  
  5. /***
  6.  *  Function    :   strelt
  7.  *
  8.  *  Description :   Return the n-th element (token) in a string.
  9.  *            The tokens are separated by several input character.
  10.  *
  11.  *  Decisions   :   If tokens are separated by ' ', '\t' or '\n',
  12.  *            the input characters will be " ".
  13.  *            If n-th token does not exist, an empty string is returned.
  14.  *
  15.  *  Parameters  :   out    char  *out_str      out string
  16.  *            in     char  *in_str       in string
  17.  *            out    char  *token        string containing token
  18.  *            in     int   element       element number
  19.  *
  20.  *  Return      :   pointer to the n-th token.
  21.  *
  22.  *  OS/Compiler :   All
  23.  */
  24.  
  25. char *strelt( char *out_str, const char *in_str, char *token, int element )
  26.  
  27. { char *ptr;
  28.  
  29.   strcpy( out_str, in_str );
  30.   ptr = strtok( out_str, token );
  31.  
  32.   while ( (element > 1) && (ptr = strtok(NULL, token)) ) element --;
  33.  
  34.   if ( ptr ) strcpy( out_str, ptr );
  35.     else *out_str = '\0';
  36.  
  37.   return out_str;
  38.  
  39. }                                              
  40.