Write a function

Write a function

Write a function that accepts a pointer to a C string as an argument and calculates the number of words contained in the string as well as the number of letters in the string. Communicate (or send) both of these values back to the main function, but DO NOT use global variables (variables defined outside of a function).

Write another function that accepts the number of letters and the number of words and sends the average number of letters per word (or average word size) back to the main function.

Demonstrate the functions in a program that asks the user to input a string. First, store the input in a large array. The program should dynamically allocate just enough memory to store the contents of that array. Copy the contents of the large array into the dynamically allocated memory. Then the program should pass that new, dynamically allocated array to the first function. Both the number of words and the average word size should be displayed on the screen. Round the average word size to 2 decimal places.

For instance, if the string argument is "Four score and seven years ago" the first function (word count) should calculate and send back a word count of 6 and a letter count of 25. The second function (average word size) should send back 4.17, or 25 / 6.

Extra challenge: See if you can prevent the program from counting punctuation (such as quotes or periods) as part of the sentence. Also, see if you can prevent the program from counting extra spaces as new words. For instance, 2 spaces will often follow a colon, such as the sentence:

"There are 3 primary colors: red, blue, and green."

In this example, the word count should be 9 (the number 3 does count as a 1-letter word), and the letter count should be 37, for an average of 4.11.

Powered by