Information
As always, please be sure to read this entire web page before doing any coding or asking any questions. You'll be glad that you did.
The average word length is 3.60 characters. This is because there are 5 words and 18 characters and 18 / 5 = 3.60. For this practice, you will distinguish between whitespace and non-whitespace.This is a simple test.
Whitespace includes spaces, tabs, and newlines (and a few others). ANY OTHER CHARACTER IS CONSIDERED PART OF A WORD. This means that all punctuation (non-whitespace) is valid for part of a word. In the example sentence above, the last word is followed by a period. This means that the number of characters in the last word is actually 5. Please make sure that you understand this before attempting this program.
The prototype for the function looks like this:
You can be guaranteed that you will only be given valid, NUL-terminated character strings.double average_word_length(const char *text);
Here is a driver file: HTML Text
You will also need one these input files: quotes-lf.txt or quotes-crlf.txt as a source of input to the program. Choose the one that matches the end-of-line characters on your system.
Typically, for Mac and Linux, it's the first one (Line Feeds only). Windows uses the second one (Carriage Return and Line Feeds). You'll have to rename the file that you download to just quotes.txt, as that is what the driver is expecting.
The name of your implementation file should be wordlen.c and the command to compile it will look like this:
gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c wordlen.c -o wordlen
Here are some sample outputs:
Approximate number of lines of code: 12.
Notes
Start by pointing at the first character. There is no leading whitespace, so you know you are at the start of a word:
Now, increment the pointer until you get to the first space:There are five words here. ^
At this point, you've found the end of the first word and 5 non-whitespace characters (5 in total). Continuing by skipping over whitespace:There are five words here. ^
Now, we've found the start of the second word. Continue, looking for the end of the word:There are five words here. ^
Now we've counted 8 non-whitespace characters. Continuing:There are five words here. ^
We've found the start of the third word. Continuing:There are five words here. ^
We've found the end of the third word and 4 more non-whitespace characters (12 characters in total). Continuing by skipping over whitespace:There are five words here. ^
We've found the start of the fourth word. Continuing:There are five words here. ^
We've found the end of the fourth word and 5 more non-whitespace characters. (17 characters in total) Continuing by skipping over whitespace:There are five words here. ^
We've found the start of the fifth word. Continuing:There are five words here. ^
We've reached the end of the string and counted 5 more characters which is 22 non-whitespace characters in total. Dividing 22 by 5 gives us and average of 4.4 characters per word.There are five words here. ^
It returns true (non-zero if the character is considered a whitespace character) and false, if not. You can see what constitutes a whitespace character here. You must include this:int isspace(intc);
which you are allowed (and expected) to do. DO NOT write your own checking function.#include <ctype.h>