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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: chrcount.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_CHRCOUNT()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Count the number of times a character appears in a string
  30.  *  $SYNTAX$
  31.  *      GT_ChrCount(<cChr>, <cStr>) --> nFreq
  32.  *  $ARGUMENTS$
  33.  *      <cChr>  - The character to find the frequence of
  34.  *      <cStr>  - The string in which to find the character
  35.  *  $RETURNS$
  36.  *      nFreq   - The number of times <cChr> occurs in <cStr>
  37.  *  $DESCRIPTION$
  38.  *      GT_ChrCount() counts how many times a specified character
  39.  *      appears in a string.
  40.  *
  41.  *      NOTE:
  42.  *         invalid parameters will return -1
  43.  *  $EXAMPLES$
  44.  *
  45.  *      ? GT_ChrCount("t", "the cat sat on the mat")      // prints 4
  46.  *
  47.  *  $END$
  48.  */
  49.  
  50. #include "extend.h"
  51.  
  52. CLIPPER
  53. gt_chrcoun()
  54. {
  55.   char *s1, *s2;
  56.   int count, pos2, len;
  57.  
  58.   if (ISCHAR(1) && ISCHAR(2)) {
  59.     s1  = _parc(1);
  60.     s2  = _parc(2);
  61.     len = _parclen(2);
  62.  
  63.     /* loop through s2 matching passed character (s1) with
  64.        each character of s1 */
  65.     for (count = 0, pos2 = 1; pos2 <= len; s2++, pos2++)
  66.       if (*s1 == *s2)               // character matches s1
  67.         count++;                    // increment counter
  68.  
  69.     _retni(count);                  // return result
  70.   } else {
  71.     _retni(-1);                     // parameter mismatch - error -1
  72.   }
  73. }
  74.