home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRREV.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  849b  |  50 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  STRREV.C - reverse a string in place
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include <string.h>
  10. #include "snip_str.h"
  11.  
  12. #if defined(__cplusplus) && __cplusplus
  13.  extern "C" {
  14. #endif
  15.  
  16. char *strrev(char *str)
  17. {
  18.       char *p1, *p2;
  19.  
  20.       if (! str || ! *str)
  21.             return str;
  22.       for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  23.       {
  24.             *p1 ^= *p2;
  25.             *p2 ^= *p1;
  26.             *p1 ^= *p2;
  27.       }
  28.       return str;
  29. }
  30.  
  31. #if defined(__cplusplus) && __cplusplus
  32.  }
  33. #endif
  34.  
  35. #ifdef TEST
  36.  
  37. #include <stdio.h>
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.       while (--argc)
  42.       {
  43.             printf("\"%s\" backwards is ", *++argv);
  44.             printf("\"%s\"\n", strrev(*argv));
  45.       }
  46.       return 0;
  47. }
  48.  
  49. #endif /* TEST */
  50.