//Zachary Abdollahi CS1A Chapter 10, P. 589, #4
/*******************************************************************************
*
* COMPUTE AVERAGE LETTERS PER WORD
*_______________________________________________________________________________
* This program accepts a C-string from the user, passes a
* pointer to the C-string to functions that count the total
* number of words and alphabetic letters, and displays the
* word count along with the average number of letters per word.
*_______________________________________________________________________________
* INPUT
* userString : C-string entered by the user
*
* OUTPUT
* wordCount : Number of words contained in the C-string
* averageLetters: Average number of letters per word
*
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
// Function Prototypes
int countWords(const char *strPtr);
int countLetters(const char *strPtr);
int main ()
{
const int SIZE = 200; // Maximum C-string size
char userString[SIZE]; //INPUT
int wordCount; //OUTPUT
int letterCount; // Total alphabetic letters in string
double averageLetters = 0.0; //OUTPUT
// Input String
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Compute Word Count and Average Letters per Word
wordCount = countWords(userString);
letterCount = countLetters(userString);
if (wordCount > 0)
{
averageLetters = static_cast<double>(letterCount) / wordCount;
}
// Output Results
cout << fixed << setprecision(1);
cout << "The string contains " << wordCount << " words." << endl;
cout << "The average number of letters per word is "
<< averageLetters << "." << endl;
return 0;
}
/*******************************************************************************
*
* FUNCTION countWords
*_______________________________________________________________________________
* This function accepts a pointer to a C-string, counts the
* number of words separated by whitespace characters, and
* returns the total word count as an integer.
*_______________________________________________________________________________
* INPUT
* strPtr : Pointer to a null-terminated C-string
*
* OUTPUT
* words : Integer count of words in the C-string
*
*******************************************************************************/
int countWords(const char *strPtr)
{
int words = 0; //OUTPUT
bool inWord = false; // Flag to track if currently inside a word
while (*strPtr != '\0')
{
if (isspace(*strPtr))
{
inWord = false;
}
else if (!inWord)
{
inWord = true;
words++;
}
strPtr++;
}
return words;
}
/*******************************************************************************
*
* FUNCTION countLetters
*_______________________________________________________________________________
* This function accepts a pointer to a C-string, counts the
* number of alphabetic letters in the string, and returns the
* total letter count as an integer.
*_______________________________________________________________________________
* INPUT
* strPtr : Pointer to a null-terminated C-string
*
* OUTPUT
* letters : Integer count of alphabetic letters
*
*******************************************************************************/
int countLetters(const char *strPtr)
{
int letters = 0; //OUTPUT
while (*strPtr != '\0')
{
if (isalpha(*strPtr))
{
letters++;
}
strPtr++;
}
return letters;
}