Question: Histogram of Word Lengths Write a C program that reads from stdin till EOF and analyzes the lengths of the words in the input. Lets
Histogram of Word Lengths
Write a C program that reads from stdin till EOF and analyzes the lengths of the words in the input. Lets consider all alphanumeric characters as words, and all non-alphanumeric characters as delimiters. For example, each of the following is a word.
Homework CS240 2
The following strings should be broken into multiple words. youll has two words: you and ll. ALLS has two words: ALL and S. UTF-8 has two words: UTF and 8.
2/19/2019 17:00 has five words: 2, 19, 2019, 17, and 00.
www.gutenberg.org has three words: www, gutenberg, and org.
You can build upon the word counting code on page 20 of K&R. As you read a word one character at a time, keep track the number of characters you have read. When you reach the end of a word, you have its length. Then you increment a counter that keeps track the number of words of this particular length. Use an array of these counters. The input to your code is /home/ming/240/hw2/CompleteShakespeare.txt. The longest word you will encounter has 27 characters.
For output, you should print 27 lines. In each line, you print the length (width 2), a space, the number of words of that length (width 6), a space, and several asterisks. Use one asterisk for each 4,000 words. If there are fewer than 4,000, you still print one asterisk, because we cannot print a fractional asterisk. Specifically, print one asterisk for 1 to 4,000 words, and two asterisks for 4,001 to 8,000 words, and so on. The asterisks constitute the histogram of word lengths. Histograms are usually printed vertically, but here it is printed horizontally because this is easier. On CompleteShakespeare.txt, your code should print exactly like Figure 1.
1 63691 2 166375 3 204211 4 223161 5121472 6 80386 7 59379 8 35083 9 20351 10 10067 11 3771 12 1353 13 454 14 247 77 16 3 17 4 18 0 19 0 20 15 1 63691 2 166375 3 204211 4 223161 5121472 6 80386 7 59379 8 35083 9 20351 10 10067 11 3771 12 1353 13 454 14 247 77 16 3 17 4 18 0 19 0 20 15
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
