HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Counting words, letters, average word length, and letter frequency

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
countingfrequencylengthwordsaverageletterwordandletters

Problem

I'm pretty sure my code is mostly correct. I think I'm having formatting errors more than anything. I keep receiving warnings about "double to float and int to float,possible loss of data. Here is what I am trying to accomplish exactly.


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 in
stance, 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

Solution

It's gonna be a small review, but do you know strtok ? As said on the link I provided :


A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

So if you want to split this into words, just put the delimiters to space and punctuation, and you already have most of the job done.

An other thing :

float avg(float words, float letters)
{
    float a = (double)(letters / words);
    return a;
}


If you want a float, why do you explicitly cast it to a double ? That's why you have warnings.

Same goes for the typing of words and letters : can it take a negative value ? If not, why does your type allows it to do so, instead of using an unsigned type ?

And last, I think you are re-inventing strlen with this piece of code : for (size = 0; array[size]; size++);. And of course you should not, just use the function instead.

Code Snippets

float avg(float words, float letters)
{
    float a = (double)(letters / words);
    return a;
}

Context

StackExchange Code Review Q#97513, answer score: 2

Revisions (0)

No revisions yet.