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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: atdiff.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_ATDIFF()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Return the position where two strings begin to differ
  30.  *  $SYNTAX$
  31.  *      GT_AtDiff(<cStr1>, <cStr2>) --> nPos
  32.  *  $ARGUMENTS$
  33.  *      <cStr1>  - A character string to compare
  34.  *      <cStr2>  - The string to compare with
  35.  *  $RETURNS$
  36.  *      nPos     - The position in <cStr2> where <cStr1> begins to differ
  37.  *  $DESCRIPTION$
  38.  *      Return the position in <cStr2> where <cStr1> begins to differ.
  39.  *      If the strings differ in the first character GT_AtDiff() will
  40.  *      return 1.  If the two strings are identical (or identical upto
  41.  *      the last character in <cStr2>) the function will return 0.
  42.  *
  43.  *      NOTE:
  44.  *         invalid parameters will return -1
  45.  *  $EXAMPLES$
  46.  *      ? gt_atDiff("the cat", "the rat")          // prints 5
  47.  *      ? gt_atDiff("the cat", "the ")             // prints 0
  48.  *
  49.  *  $END$
  50.  */
  51.  
  52.  
  53. #include "extend.h"
  54.  
  55. CLIPPER
  56. gt_atdiff()
  57. {
  58.   char *s1, *s2;
  59.   int pos, len;
  60.  
  61.   if (ISCHAR(1) && ISCHAR(2)) {
  62.     s1  = _parc(1);
  63.     s2  = _parc(2);
  64.     len = _parclen(2);
  65.  
  66.     /*
  67.        loop through comparing both strings
  68.  
  69.        NOTE: pos starts at 1, so as to return a string index
  70.              for CLIPPER
  71.     */
  72.  
  73.     for (pos = 1; (pos <= len) && (*s1 == *s2); s2++, s1++)
  74.       pos++;
  75.  
  76.     if (pos > len)                  // strings match exactly!!!
  77.       _retni(0);
  78.     else
  79.       _retni(pos);
  80.   } else {
  81.     _retni(-1);                     // parameter mismatch - error -1
  82.   }
  83. }
  84.