Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int maxHeight = 0;
  5. int maxLength = 5;
  6. int * lengths;
  7.  
  8. void addLength(int length)
  9. {
  10.     if (length > maxLength)
  11.     {
  12.         lengths = realloc(lengths, length * sizeof(int));
  13.         for (int i = maxLength; i < length; ++i)
  14.             lengths[i] = 0;
  15.         maxLength = length;
  16.     }
  17.     int height = ++lengths[length-1];
  18.     if (height > maxHeight)
  19.         maxHeight = height;
  20.    
  21. }
  22.  
  23. int main()
  24. {
  25.     lengths = malloc(maxLength * sizeof(int));
  26.     char c;
  27.     int wordlength = 0;
  28.     while((c = getchar()) != EOF)
  29.     {
  30.         if (c == '\n')
  31.         {
  32.             addLength(wordlength);
  33.            
  34.             int length = maxLength;
  35.             int width = 1;
  36.             while (length)
  37.             {
  38.                 length = length/10;
  39.                 width++;
  40.             }
  41.            
  42.             for (int i = maxHeight-1; i >= 0; --i)
  43.             {
  44.                 for (int word = 0; word < maxLength; ++word)
  45.                 {
  46.                     if (lengths[word] > i)
  47.                     {
  48.                         for (int j = 0; j < width; j++)
  49.                             printf("*");
  50.                     }
  51.                     else
  52.                     {
  53.                         for (int j = 0; j < width; j++)
  54.                             printf(" ");
  55.                     }
  56.                     printf(" ");
  57.                 }
  58.                 printf("\n");
  59.             }
  60.             for (int word = 0; word < maxLength; ++word)
  61.             {
  62.                 printf("%*d ", width,word+1);
  63.             }
  64.             printf("\n");
  65.         }
  66.         else if(c == ' ') // found a new word
  67.         {
  68.             addLength(wordlength);
  69.             wordlength = 0;
  70.         }
  71.         else if (c != ' ') //found a char
  72.         {
  73.             wordlength++;
  74.         }
  75.        
  76.     }
  77.     free(lengths);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement