С программу для подсчета количества слов в строке

Как написать программу C , чтобы подсчитать количество слов в строке в C Programming Language ?


Эта программа С до Подсчитайте количество слов в строке.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Подсчитать количество слов в строке
  6. int wcount(char *s)
  7. {
  8.     char *= s;
  9.     int count = 0;
  10.  
  11.     while (1)
  12.     {
  13.         while (*== ' ')
  14.             p++;
  15.         if (!*p)
  16.             break;
  17.         count++;
  18.         while (*&& (*!= ' '))
  19.             p++;
  20.     }
  21.  
  22.     return count;
  23. }
  24.  
  25. void GetWordIndex(char *s, int *count)
  26. {
  27.     int size = 0;
  28.  
  29.     int i = 0;
  30.     while (1)
  31.     {
  32.         while (s[i] == ' ')
  33.                 i++;
  34.  
  35.         if (s[i] == '\0')
  36.                 break;
  37.  
  38.         count[size] = i;
  39.         size++;
  40.  
  41.         while (s[i] != '\0' && s[i] != ' ')
  42.                 i++;
  43.  
  44.         if (s[i] == '\0')
  45.                 break;
  46.     }
  47. }
  48.  
  49.  
  50. void csort(char *src, char *dest)
  51. {
  52.     int len = strlen(src);
  53.     int n = wcount(src);
  54.  
  55.     int count[n];
  56.     int new_count[len];
  57.     int j;
  58.     for (= 0; j < len; j++)
  59.         new_count[j] = 0;
  60.  
  61.     char *= src;
  62.  
  63.     GetWordIndex(src, count);
  64.     int i;
  65.  
  66.     for (= 0; i < len, p[i] != '\0' && p[i] != ' '; i++)
  67.         for (= 0; j < n; j++)
  68.             new_count[count[j]]++;
  69.  
  70.     for (= 0; i < n; i++)
  71.         printf("%d, ", new_count[i]);
  72.  
  73.  
  74. }
  75.  
  76. int main()
  77. {
  78.     char *scr = (char *)malloc(1024);
  79.     gets(scr);
  80.     char *dest = (char *)malloc(1024);
  81.     csort(scr, dest);
  82.  
  83.     free(scr);
  84.     free(dest);
  85.     return 0;
  86. }


Learn More :