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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: strdiff.c
  5.  * Author....: Andy M Leighton
  6.  * BBS.......: The Dark Knight Returns
  7.  * Net/Node..: 050/069
  8.  * User Name.: Andy Leighton
  9.  * Date......: 24/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_STRDIFF()
  26.  *  $CATEGORY$
  27.  *      String
  28.  *  $ONELINER$
  29.  *      Return a string where it begins to differ from another
  30.  *  $SYNTAX$
  31.  *      GT_StrDiff(<cStr1>, <cStr2>) --> cRet
  32.  *  $ARGUMENTS$
  33.  *      <cStr1>  - A character string to compare
  34.  *      <cStr2>  - The string to compare with
  35.  *  $RETURNS$
  36.  *      cRet     - A string beginning at the position in <cStr2> where
  37.  *                 <cStr1> begins to differ from <cStr1>
  38.  *  $DESCRIPTION$
  39.  *      Return a string beginning at the position in <cStr2> where
  40.  *      <cStr1> begins to differ from <cStr1>. If the two strings are
  41.  *      identical (or identical upto the last character in <cStr2>)
  42.  *      the function will return "".
  43.  *
  44.  *      NOTE:
  45.  *         invalid parameters will return ""
  46.  *  $EXAMPLES$
  47.  *      ? gt_strDiff("the cat", "the rat")          // prints "rat"
  48.  *      ? gt_strDiff("the cat", "the ")             // prints ""
  49.  *
  50.  *  $END$
  51.  */
  52.  
  53. #include "extend.h"
  54.  
  55. CLIPPER
  56. GT_strdiff()
  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.       _retc((char *) NULL);
  78.     else
  79.       _retc(s2);
  80.   } else {
  81.     _ret();                         // parameter mismatch - error return NIL
  82.   }
  83. }
  84.