home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / chrfirst.c < prev    next >
C/C++ Source or Header  |  1993-10-14  |  2KB  |  82 lines

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: chrfirst.c
  5.  * Author....: Andy M Leighton
  6.  * BBS.......: The Dark Knight Returns
  7.  * Net/Node..: 050/069
  8.  * User Name.: Andy Leighton
  9.  * Date......: 23/05/93
  10.  * Revision..: 1.00
  11.  *
  12.  * This is an original work by Andy Leighton and is placed in the
  13.  * public domain.
  14.  *
  15.  * Modification history:
  16.  * ---------------------
  17.  *
  18.  * $Log$
  19.  *
  20.  */
  21.  
  22. /*
  23.  *  $DOC$
  24.  *  $FUNCNAME$
  25.  *      GT_CHRFIRST()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Find which character occurs first in a string
  30.  *  $SYNTAX$
  31.  *      GT_ChrFirst(<cChars>, <cStr>) --> nAsc
  32.  *  $ARGUMENTS$
  33.  *      <cChars> - The set of characters to find
  34.  *      <cStr>   - The input string
  35.  *  $RETURNS$
  36.  *      nAsc     - The ASCII value of the first character in <cChars>
  37.  *                 which appears first in <cStr>
  38.  *  $DESCRIPTION$
  39.  *      Return the ascii value of a character in <cChars>
  40.  *      which appears first in <cStr>.
  41.  *  $EXAMPLES$
  42.  *
  43.  *      ? chr(GT_ChrFirst("sa ", "This is a test"))  // prints "s"
  44.  *      ? chr(GT_ChrFirst("et",  "This is a test"))   // prints "t"
  45.  *
  46.  *  $END$
  47.  */
  48.  
  49. #include "extend.h"
  50.  
  51. CLIPPER
  52. GT_ChrFirst()
  53. {
  54.   char *string;
  55.   char *cset;
  56.   int l1, l2;
  57.   int p1, p2;
  58.  
  59.   if (ISCHAR(1) && ISCHAR(2)) {
  60.     string = _parc(2);
  61.     cset   = _parc(1);
  62.     l1     = _parclen(2);
  63.     l2     = _parclen(1);
  64.     p1     = p2 = 0;
  65.  
  66.     do {
  67.       for (p2 = 0; (p2 < l2) && (cset[p2] != string[p1]); ++p2)
  68.          ;
  69.       if (p2 < l2) {
  70.          _retni(string[p1]);
  71.          break;
  72.       }
  73.     } while (p1++ < l1);
  74.  
  75.     if (p2 >= l2)
  76.       _retni(0);
  77.  
  78.   } else {
  79.     _retni(-1);               // parameter mismatch - error NullStr
  80.   }
  81. }
  82.