Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. //#define NEWLINE 13
  6. #define NEWLINE 10
  7.  
  8. static char** groups;   // Array with the groups of letters.
  9. static int    n_groups; // Current amount of groups (used for realloc())
  10. static int*   weights;  // Array with the precalculated # of 'a' in each group
  11. static char*  group;    // String representing the current group
  12. static int    n_group;  // Current amount of letters in the group
  13.  
  14. int add_to_group(char c)
  15. {
  16.     // Allocate memory:
  17.     if (n_group == 0)
  18.     {
  19.         n_group++;
  20.         group = (char*) malloc(sizeof(char) * 1);
  21.     }
  22.     else
  23.     {
  24.         n_group++;
  25.         group = (char*) realloc(group, sizeof(char) * n_group);
  26.     }
  27.  
  28.     // Store the new symbol:
  29.     group[n_group - 1] = c;
  30.  
  31.     return 0;
  32. }
  33.  
  34. int store_group(void)
  35. {
  36.     int a, i;
  37.  
  38.     // Allocate memory:
  39.     if (n_groups == 0)
  40.     {
  41.         n_groups++;
  42.         weights = (int*  ) malloc(sizeof(int  ) * 1);
  43.         groups  = (char**) malloc(sizeof(char*) * 1);
  44.     }
  45.     else
  46.     {
  47.         n_groups++;
  48.         weights = (int*  ) realloc(weights, sizeof(int  ) * n_groups);
  49.         groups  = (char**) realloc(groups , sizeof(char*) * n_groups);
  50.     }
  51.     // Finish the group with the \0 symbol
  52.     add_to_group(0);
  53.  
  54.     // Count the 'a' symbols and store the data:
  55.     for (i = 0; ; ++i)
  56.     {
  57.         if (group[i] == 0)
  58.             break;
  59.         if (group[i] == 'a')
  60.             a++;
  61.     }
  62.     groups [n_groups - 1] = group;
  63.     weights[n_groups - 1] = a;
  64.  
  65.     // Set n_group to 0 to indicate the start of the new group
  66.     n_group = 0;
  67.  
  68.     return 0;
  69. }
  70.  
  71. int sort_groups(void)
  72. {
  73.     int flag;
  74.     int i;
  75.     int   int_temp;
  76.     char* char_p_temp;
  77.  
  78.     flag = 0;
  79.  
  80.     for (; flag != 1;)
  81.     {
  82.         flag = 1;
  83.         for (i = 1; i < n_groups; ++i)
  84.             if (weights [i - 1] < weights [i])
  85.             {
  86.                 int_temp        = weights [i - 1];
  87.                 weights [i - 1] = weights [i    ];
  88.                 weights [i    ] = int_temp;
  89.  
  90.                 char_p_temp    = groups [i - 1];
  91.                 groups [i - 1] = groups [i    ];
  92.                 groups [i    ] = char_p_temp;
  93.  
  94.                 flag = 0;
  95.             }
  96.     }
  97.  
  98.     return 0;
  99. }
  100.  
  101. int free_groups(void)
  102. {
  103.     int i;
  104.     for (i = 0; i < n_groups; ++i)
  105.         free(groups[i]);
  106.     free(groups);
  107.  
  108.     return 0;
  109. }
  110.  
  111. int main(void)
  112. {
  113.     int true_int, flag;
  114.     char c;
  115.     int i, j;
  116.  
  117.     flag = 0;
  118.     true_int = 1;
  119.  
  120.     groups   = NULL;
  121.     n_groups = 0;
  122.     weights  = NULL;
  123.     group    = NULL;
  124.     n_group  = 0;
  125.  
  126.     while (true_int)
  127.     {
  128.         scanf("%c", &c);
  129.         if (c == 0) break;
  130.         if (c == NEWLINE) break;
  131.  
  132.         if (isalpha(c))
  133.         {
  134.             flag = 1;
  135.             add_to_group(c);
  136.         }
  137.         else
  138.         {
  139.             if (flag)
  140.             {
  141.                 flag = 0;
  142.                 store_group();
  143.             }
  144.         }
  145.     }
  146.  
  147.     sort_groups();
  148.  
  149.     for (i = 0; i < n_groups; ++i)
  150.     {
  151.         for (j = 0; ; ++j)
  152.         {
  153.             if (groups[i][j] == 0)
  154.                 break;
  155.             printf("%c", groups[i][j]);
  156.         }
  157.         printf(" ");
  158.     }
  159.  
  160.     free_groups();
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement