home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / STRREV.C < prev    next >
Text File  |  1984-12-31  |  1KB  |  41 lines

  1. /*  File   : strrev.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 1 June 1984
  4.     Defines: strrev()
  5.  
  6.     strrev(dst, src)
  7.     copies all the characters of src to dst, in REVERSE order.   Dst is
  8.     properly terminated with a NUL character.  There is no result.
  9.     Example: strrev(x, "able was I er") moves "re I saw elba" to x.
  10.  
  11.     Note: this function is perfectly happy to reverse a string into the
  12.     same place, strrev(x, x) will work.  That is why it looks so hairy.
  13.     It will not work for partially overlapping source and destination.
  14. */
  15.  
  16. #include "strings.h"
  17.  
  18. void strrev(dsta, srca)
  19.     register char *dsta, *srca;
  20.     {
  21.     register char *dstz, *srcz;
  22.     register int t;            /* should be char */
  23.  
  24.     for (srcz = srca; *srcz++; ) ;
  25.     srcz--;
  26.     dstz = dsta+(srcz-srca);
  27.     /*  Now srcz points to the NUL terminating src,
  28.         and dstz points to where the terminating NUL for dst belongs.
  29.     */
  30.     *dstz = NUL;
  31.     while (srcz > srca) {
  32.         /*  This is guaranteed safe by K&R, since srcz and srca
  33.         point "into the same array".
  34.         */
  35.         t = *--srcz;
  36.         *--dstz = *srca++;
  37.         *dsta++ = t;
  38.     }
  39.     }
  40.