1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
   |  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
size_t str_count_word (const char *str, const char *sep)
{
   size_t nb = 0;
   char *z = strdup (str);
   if (z != NULL)
   {
      char const *s = strtok (z, sep);
      if (s != NULL)
      {
         nb += 1;
         while ((s = strtok (NULL, sep)) != NULL)
            nb += 1;
      }
      free (z);
   }
   return nb;
}
 
int main (void)
{
   struct test
   {
      int nt;
 
      struct
      {
         char const *s;
      }
      in;
      struct
      {
         size_t n;
      }
      out;
   };
 
   static struct test const at[] = {
      /* *INDENT-OFF* */
      {10, {""       },{0},},
      {11, {";"      },{0},},
      {12, {";;"     },{0},},
 
      {20, {"a;"     },{1},},
      {21, {";a"     },{1},},
      {22, {";a;"    },{1},},
      {23, {";;a"    },{1},},
      {24, {"a;;"    },{1},},
      {25, {";a;;"   },{1},},
      {26, {";;a;;"  },{1},},
 
      {30, {"a;b"     },{2},},
      {31, {";a;b"    },{2},},
      {32, {";a;;b"   },{2},},
      {33, {";;aa;b"  },{2},},
      {34, {"a;;b"    },{2},},
      {35, {";a;;b"   },{2},},
      {36, {";;a;;b"  },{2},},
 
      {40, {";a;b;"     },{2},},
      {41, {";a;b ;"    },{2},},
      {42, {";a; b;"    },{2},},
      {43, {";a; b ;"   },{2},},
      {44, {";a ;b;"    },{2},},
      {45, {";a ;b ;"   },{2},},
      {46, {";a ; b;"   },{2},},
      {47, {";a ; b ;"  },{2},},
      {48, {"; a;b;"    },{2},},
      {49, {"; a;b ;"   },{2},},
      {50, {"; a; b;"   },{2},},
      {51, {"; a; b ;"  },{2},},
      {52, {"; a ;b;"   },{2},},
      {53, {"; a ;b ;"  },{2},},
      {54, {"; a ; b;"  },{2},},
      {55, {"; a ; b ;" },{2},},
 
      /* *INDENT-ON* */
   };
 
   size_t i;
 
   for (i = 0; i < sizeof at / sizeof *at; i++)
   {
      struct test const *pt = at + i;
      /* D.U.T */
      size_t nb = str_count_word (pt->in.s, ";");
 
      if (nb != pt->out.n)
      {
         printf ("ERROR at test #%d\n", pt->nt);
         printf ("'%s' = %d\n", pt->in.s, nb);
         break;
      }
   }
 
   if (i == sizeof at / sizeof *at)
   {
      puts ("P A S S E D");
   }
   return 0;
} | 
Partager