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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  TRIM.C - Remove leading, trailing, & excess embedded spaces
  5. **
  6. **  public domain by Bob Stout & Michael Dehlwes
  7. */
  8.  
  9. #include <ctype.h>
  10. #include "snip_str.h"
  11.  
  12. #if defined(__cplusplus) && __cplusplus
  13.  extern "C" {
  14. #endif
  15.  
  16. char *trim (char *str)
  17. {
  18.       char *ibuf, *obuf;
  19.  
  20.       if (str)
  21.       {
  22.             for (ibuf = obuf = str; *ibuf; )
  23.             {
  24.                   while (*ibuf && (isspace (*ibuf)))
  25.                         ibuf++;
  26.                   if (*ibuf && (obuf != str))
  27.                         *(obuf++) = ' ';
  28.                   while (*ibuf && (!isspace (*ibuf)))
  29.                         *(obuf++) = *(ibuf++);
  30.             }
  31.             *obuf = NUL;
  32.       }
  33.       return (str);
  34. }
  35.  
  36. #if defined(__cplusplus) && __cplusplus
  37.  }
  38. #endif
  39.  
  40. #ifdef TEST
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44.  
  45. int main (int argc, char *argv[])
  46. {
  47.       if (argc == 2)
  48.       {
  49.             printf ("trim(\"%s\")\n", argv[1]);
  50.             printf ("returned \"%s\"\n", trim (argv[1]));
  51.       }
  52.       else
  53.       {
  54.             fprintf (stderr, "To test this function, call TRIM\n");
  55.             fprintf (stderr, "with an argument enclosed in quotes.\n");
  56.             fprintf (stderr, "   Example:\n");
  57.             fprintf (stderr, "   C:\\>trim \"  test   test   \"\n");
  58.             fprintf (stderr, "   trim(\"  test   test   \"\n");
  59.             fprintf (stderr, "   returned \"test test\"\n\n");
  60.             fprintf (stderr, "   C:\\>_\n");
  61.             return EXIT_FAILURE;
  62.       }
  63.       return EXIT_SUCCESS;
  64. }
  65.  
  66. #endif /* TEST */
  67.