Write a function that accepts a pointer to a C-string as an argument and calcula
ID: 3552988 • Letter: W
Question
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.
Explanation / Answer
#include <string.h>
int wordCount(char cstring[]) {
int count = 0;
char *pch;
pch = strtok(cstring, " ");
while (pch != NULL) {
count++;
pch = strtok(NULL, " ");
}
return count;
}
For facing challenges,the above code may not work
Try the following
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib> #include <string>using namespace std;
void wordCount(char [], int&, double&, int&);
double get_avg(int, int, double);
int main()
{
int word = 0, Punct = 0;
double AllChar = 0.0;
double avg = 0.0;
char cstring[81];
string cstring;
cout << "Enter a string of 80 or fewer characters" << endl; cout << "Enter a string" << endl; cout << "=> ";
cin.getline(cstring, 81);
getline(cin, cstring);
// The remaining changes needed are left as an exercise for the
reader...
wordCount(cstring, word, AllChar, Punct);
cout <<" The number of words in that string: " << word << endl;
avg = get_avg(word, Punct, AllChar);
cout <<" Average number of characters per word: "<< fixed <<
showpoint << setprecision(2) << avg << " " << endl;
return 0;
}
// wordCount() counts total character, words, and Puctuation
void wordCount(char cstring[],int &word, double &AllChar, int &Punct)
{
int index = 0;
while (cstring[index] != '')
{
if ((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
while((isspace(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
if ((isalnum(cstring[index])) || (ispunct(cstring[index])))
{
word++;
while
((isalnum(cstring[index]))||(ispunct(cstring[index])))
{
index++;
AllChar++; //Counting total printable character(including digits
and Punctuation
if((ispunct(cstring[index])))
{
Punct++; // Counting Punctuation
}
}
}
index++;
}
}
// get_avg() calculates the average number of characters per words
double get_avg(int word,int Punct, double AllChar )
{
double avg = 0.0;
AllChar = AllChar - Punct; // Subtracting Punctuatoin from All the
characters in the string(not including spaces)
avg = (AllChar / word);
return avg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.