home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_08 / 1108054a < prev    next >
Text File  |  1993-05-23  |  3KB  |  113 lines

  1. #include <stdio.h>
  2.  
  3. /* strws.c - string function to replace blanks and tabs
  4.  * with a single blank. */
  5. char *strws(char *str) 
  6. {
  7.    char *from = str;    /* pointer to destination */
  8.    char *to = str;        /* pointer to source      */
  9.  
  10.    if (str != NULL) {
  11.        /* copy characters looking for a blank or a tab. */
  12.       while (*from) {
  13.          if (*from == ' ' || *from == '\t') {
  14.             /* if blank or tab is found, copy one blank to 
  15.              * the destination and skip any other blanks or
  16.              * tabs after the one we found. */
  17.             *to++ = ' ';  from++;
  18.             while (*from)
  19.                if (*from != ' ' && *from != '\t')
  20.                   break;
  21.                 else
  22.                   from++;
  23.          }
  24.  
  25.          /* whether we found a blank (or tab) above or not,
  26.           * we are not pointing to one now, so copy the 
  27.           * character we are pointing to and continue. */
  28.  
  29.          *to++ = *from++;
  30.       }
  31.       *to = '\0';       /* terminate new string */
  32.    }
  33.    return str;
  34. } /*strws*/
  35.  
  36.  
  37. #ifdef TESTMAIN
  38.  
  39. #define MAXSIZE  1024
  40.  
  41. int errors = 0;        /* count of errors encountered */
  42. char *rv;        /* value returned by call to strws */
  43. char buf[MAXSIZE];    /* buffer to hold result of each test case */
  44. int testcase = 0;    /* test case counter */
  45.  
  46. /* testin contains a list of test cases */
  47. char *testin[] = {
  48.     "This has no extra white space ",
  49.     "                    ",  
  50.     "This \t has\t \ta\tmixture\t \t \t \t",          
  51.     "\tLeading and  trailing tab\t",
  52.     "\t\t\t\tLeading and  trailing tabs\t\t\t\t",
  53.     "       Leading and \ttrailing blanks     ",
  54.     ""
  55.     };
  56.  
  57. /* testout contains the expected result of sending the corresponding
  58.  * element in testin to strws. */
  59. char *testout[] = {
  60.     "This has no extra white space ",
  61.     " ",
  62.     "This has a mixture ",
  63.     " Leading and trailing tab ",
  64.     " Leading and trailing tabs ",
  65.     " Leading and trailing blanks ",
  66.     ""
  67.     };
  68.  
  69. main()
  70. {
  71.    printf("strws test: starting\n");
  72.    if ((rv = strws((char *) 0)) != NULL)
  73.    {
  74.       printf("ERROR 1: strws(NULL) returned %p\n",rv);
  75.       printf("                     expected NULL\n");
  76.       errors++;
  77.    }
  78.     
  79.    do {
  80.  
  81.       /* make a copy of a test case */
  82.       strcpy(buf, testin[testcase]);
  83.  
  84.       /* run the test and see if strws returns a pointer to
  85.        * the beginning of the atring as it should. */
  86.       if ((rv = strws(buf)) != buf)  {
  87.          printf("ERROR 2: case %d:strws(%s)\nreturned %p\n",
  88.                  testcase, buf, rv);      
  89.          errors++;
  90.       }
  91.  
  92.       /* see if the result obtained is the same as the expected 
  93.        * result. */
  94.       if (strcmp(buf, testout[testcase]) != 0) {
  95.           printf("ERROR 3: case %d:strws(%s)", testcase, 
  96. testin[testcase]);
  97.           printf("  returned [%s]\n", buf);
  98.           printf("  expected [%s]\n", testout[testcase]);
  99.           errors++;
  100.       }
  101.    } while (testin[testcase++][0] != 0);
  102.  
  103.    if (errors)
  104.       printf("strws: Failed, %d errors encountered.\n", errors);
  105.    else
  106.       printf("strws: SUCCEEDED\n");
  107.  
  108.    return 0;
  109. } /*main*/
  110.  
  111. #endif
  112.  
  113.