Hello all! I am working on an assignment but have run into some issues, on the very last day its due of course :( I was wondering if I could have someone take a look at my code and tell me how I can turn my data into a dynamic array and utilize the rest of the functions I wrote. Any help would be appreciated, thank you so much!
Here is the directions:
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 both of these values back to the main function, but DO NOT use global variables.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
void Count_All(char*, int&, double&, int&); // Function prototype.
double Calc_Average (char*, int, int, double); // Function prototype.
int main()
{
const int size = 500;
char userString[size];
int Word = 0;
int Pun = 0;
double Total_Characters = 0;
double Average = 0.0;
cout << "Please enter a string of 500 or less characters: ";
cin.getline(userString, size);
cout << "\n";
Count_All (userString, Word, Total_Characters, Pun);
cout << "Number of words in the string: " << Word << "\n";
Average = Calc_Average (userString, Word, Pun, Total_Characters);
cout <<"\nAverage number of letters per word: "<< fixed <<
showpoint << setprecision(2) << Average << "\n" << endl;
cin.ignore(1);
return 0;
}
void Count_All (char*strptr, int &Word, double &Total_Characters, int &Pun) // Counts all characters and types.
{
int index = 0;
while (*strptr != '\0')
{
if ((isspace(*strptr)) || (ispunct(*strptr)))
{
while ((isspace(*strptr)) || (ispunct(*strptr)))
{
index++;
}
}
if ((isalnum(*strptr)) || (ispunct(*strptr)))
{
Word++;
while ((isalnum(*strptr))||(ispunct(*strptr)))
{
index++;
Total_Characters++; // Counting the total printable characters (including digits and punctuation).
if((ispunct(*strptr)))
{
Pun++; // Counting punctuation.
}
}
}
index++;
}
}
double Calc_Average(char*strptr, int Word, int Pun, double Total_Characters) // Calculates the average number of characters per words.
{
double Average = 0.0;
Total_Characters = Total_Characters - Pun; // Subtracting punctuation from all of the characters in the string (not including spaces).
Average = (Total_Characters / Word);
return Average;
}
|