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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: chrtotal.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_CHRTOTAL()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Find number of times a set of characters appears in a string
  30.  *  $SYNTAX$
  31.  *      GT_ChrTotal(<cChrs>, <cStr>) --> nTotOcc
  32.  *  $ARGUMENTS$
  33.  *      <cChrs> - The set of characters
  34.  *      <cStr>  - The string to search
  35.  *  $RETURNS$
  36.  *      nTotOcc - The number of times the characters specified in
  37.  *                <cChrs> appears in <cStr>
  38.  *  $DESCRIPTION$
  39.  *      Returns the numnber of occurrences of characters belonging
  40.  *      to the set <cChrs> in the string <cStr>.  If no characters
  41.  *      in <cChrs> appears in <cStr> GT_ChrTotal() will return 0.
  42.  *
  43.  *      NOTE:
  44.  *         invalid parameters will return -1
  45.  *  $EXAMPLES$
  46.  *
  47.  *       local cStr1 := "the cat sat on the mat"
  48.  *
  49.  *       ? GT_ChrTotal("tae", cStr1)            // prints 10
  50.  *       ? GT_ChrTotal("zqw", cStr1)            // prints  0
  51.  *  $END$
  52.  */
  53.  
  54. #include "extend.h"
  55.  
  56. CLIPPER
  57. gt_chrtotal()
  58. {
  59.   char *s1, *s2;
  60.   int count, p1, p2, l2, l1;
  61.  
  62.   if (ISCHAR(1) && ISCHAR(2)) {
  63.     s1  = _parc(1);
  64.     s2  = _parc(2);
  65.     l2  = _parclen(2);
  66.     l1  = _parclen(1);
  67.  
  68.     for (count = 0, p2 = 0; p2 < l2; p2++)
  69.       for (p1 = 0; p1 < l1; p1++)
  70.         if (s1[p1] == s2[p2])
  71.           count++;                    // increment counter
  72.  
  73.     _retni(count);                  // return result
  74.   } else {
  75.     _retni(-1);                     // parameter mismatch - error -1
  76.   }
  77. }
  78.