Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define LIMIT 20
- #define IN 1
- #define OUT 0
- int main(void)
- {
- /*
- * The freq array will be used to store a frequency of values associated to a natural
- * number from 1 to 20 included. The word variable indicates whether the c variable
- * currently holds a value that is inside or outside of a word. The count variable
- * keeps count of the current number of characters inside the word we're evaluating.
- * The toolong variable is just there to prevent a segfault in case the word exceeds
- * the number of elements included in the freq array, so that we don't increment
- * a nonexistent value.
- */
- char c;
- int freq[LIMIT];
- for (int i = 0; i <= LIMIT; ++i) freq[i] = 0;
- int word = OUT;
- int count = 0;
- int toolong = 0;
- /*
- * Evaluate the current character's ASCII value:
- * 1) If it's neither an uppercase nor a lowercase letter, then it's not a word.
- * if the number of letters in the current word doesn't exceed the limit,
- * then increment the freq array's element corresponding to that word's length.
- * Reinitialize the counter so that the non-letter character isn't registered.
- * 2) If we're not currently inside a word, yet the ASCII value of the current char
- * corresponds to a letter, then we've just entered a word. Reinitialize the counter.
- * Increment it so that a word with x letters isn't registered as having x-1 letters.
- * 3) If we're inside a word, then increment the counter.
- */
- while ((c = getchar()) != EOF) {
- if ((c < 'A' || c > 'z') || (c > 'Z' && c < 'a')) {
- word = OUT;
- if (count <= LIMIT) ++freq[count];
- else ++toolong;
- count = 0;
- }
- else if (word == OUT) {
- word = IN;
- count = 0;
- ++count;
- }
- else ++count;
- }
- /*
- * Prints all natural numbers from 1 to 20, and next to each number, prints one
- * '*' character for every instance of a word containing that number of characters.
- * Then, print the number of words that were too long to be taken into account.
- */
- for (int i = 1; i <= LIMIT; ++i) {
- printf("| %2d | ", i);
- for (int f = 1; f <= freq[i]; ++f) printf("*");
- putchar('\n');
- }
- printf("Number of words that were too long to be taken into account: %d\n", toolong);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement