home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / STRNREV.C < prev    next >
C/C++ Source or Header  |  1984-12-31  |  2KB  |  45 lines

  1. /*  File   : strnrev.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 1 June 1984
  4.     Defines: strnrev()
  5.  
  6.     strnrev(dst, src, len)
  7.     copies all the characters of src to dst, in REVERSE order.  If src
  8.     was terminated by a NUL character, so will dst be, otherwise dst &
  9.     src are both exactly len non-NUL characters long.  This returns no
  10.     result.  It is to strrev as strncpy is to strcpy.
  11.  
  12.     Note: this function is perfectly happy to reverse a string into the
  13.     same place, strnrev(x, x, L) will work.
  14.     It will not work for partially overlapping source and destination.
  15. */
  16.  
  17. #include "strings.h"
  18.  
  19. void strnrev(dsta, srca, len)
  20.     register char *dsta, *srca;
  21.     register int len;
  22.     {
  23.     register char *dstz, *srcz;
  24.     register int t;
  25.     /*  On a machine which doesn't supply 6 register variables,
  26.         you could #define t len, as the two variables don't overlap.
  27.     */
  28.  
  29.     for (srcz = srca; --len >= 0 && *srcz; srcz++) ;
  30.     dstz = dsta+(srcz-srca);
  31.     /*  If srcz was stopped by len running out, it points just after
  32.         the last character of the source string, and it and dstz are
  33.         just right.  Otherwise, it was stopped by hitting NUL, and is
  34.         in the right place, but dstz should get a NUL as well.
  35.     */
  36.     if (len >= 0) *dstz = NUL;
  37.     /*  That was the very last use of len  */
  38.     while (srcz > srca) {
  39.         t = *--srcz;
  40.         *--dstz = *srca++;
  41.         *dsta++ = t;
  42.     }
  43.     }
  44.