Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int maxHeight = 0;
  5. int maxLength = 10;
  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.             for (int i = maxHeight-1; i >= 0; --i)
  35.             {
  36.                 for (int word = 0; word < maxLength; ++word)
  37.                 {
  38.                     if (lengths[word] > i)
  39.                         printf("*");
  40.                     else
  41.                         printf(" ");
  42.                 }
  43.                 printf("\n");
  44.             }
  45.         }
  46.         else if(c == ' ') // found a new word
  47.         {
  48.             addLength(wordlength);
  49.             wordlength = 0;
  50.         }
  51.         else if (c != ' ') //found a char
  52.         {
  53.             wordlength++;
  54.         }
  55.        
  56.     }
  57.     free(lengths);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement