Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming Help Write a function that accepts a pointer to a C-string as an

ID: 3680198 • Letter: C

Question

C++ Programming Help

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.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
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];
cout << "Enter a string of 80 or fewer " << endl;
cout << "=> ";
cin.getline(cstring, 81);
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;
}
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++;
if((ispunct(cstring[index])))
{
Punct++;
}
}
}
index++;
}
}
double get_avg(int word,int Punct, double AllChar )
{
double avg = 0.0;
AllChar = AllChar - Punct;
avg = (AllChar / word);
return avg;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote