Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #define LIMIT 20
  3. #define IN 1
  4. #define OUT 0
  5.  
  6. int main(void)
  7. {
  8.     /*
  9.      * The freq array will be used to store a frequency of values associated to a natural
  10.      * number from 1 to 20 included. The word variable indicates whether the c variable
  11.      * currently holds a value that is inside or outside of a word. The count variable
  12.      * keeps count of the current number of characters inside the word we're evaluating.
  13.      * The toolong variable is just there to prevent a segfault in case the word exceeds
  14.      * the number of elements included in the freq array, so that we don't increment
  15.      * a nonexistent value.
  16.     */
  17.     char c;
  18.     int freq[LIMIT];
  19.     for (int i = 0; i <= LIMIT; ++i) freq[i] = 0;
  20.     int word = OUT;
  21.     int count = 0;
  22.     int toolong = 0;
  23.  
  24.     /*
  25.      * Evaluate the current character's ASCII value:
  26.      * 1) If it's neither an uppercase nor a lowercase letter, then it's not a word.
  27.      * if the number of letters in the current word doesn't exceed the limit,
  28.      * then increment the freq array's element corresponding to that word's length.
  29.          * Reinitialize the counter so that the non-letter character isn't registered.
  30.      * 2) If we're not currently inside a word, yet the ASCII value of the current char
  31.      * corresponds to a letter, then we've just entered a word. Reinitialize the counter.
  32.          * Increment it so that a word with x letters isn't registered as having x-1 letters.
  33.          * 3) If we're inside a word, then increment the counter.
  34.     */
  35.     while ((c = getchar()) != EOF) {
  36.         if ((c < 'A' || c > 'z') || (c > 'Z' && c < 'a')) {
  37.             word = OUT;
  38.             if (count <= LIMIT) ++freq[count];
  39.             else ++toolong;
  40.             count = 0;
  41.         }
  42.         else if (word == OUT) {
  43.             word = IN;
  44.             count = 0;
  45.             ++count;
  46.         }
  47.         else ++count;
  48.     }
  49.  
  50.     /*
  51.      * Prints all natural numbers from 1 to 20, and next to each number, prints one
  52.      * '*' character for every instance of a word containing that number of characters.
  53.      * Then, print the number of words that were too long to be taken into account.
  54.     */
  55.     for (int i = 1; i <= LIMIT; ++i) {
  56.         printf("| %2d | ", i);
  57.         for (int f = 1; f <= freq[i]; ++f) printf("*");
  58.         putchar('\n');
  59.     }
  60.     printf("Number of words that were too long to be taken into account: %d\n", toolong);
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement