//Sam Trivikraman CS1A Chapter 10, p. 589, #4
//
/*
******************************************************************************
Count The Number of Words In A String (#3 Modified)
_______________________________________________________________________________
This program counts and displays the number of words and average number
of letters in a string.
_______________________________________________________________________________
INPUT
the string of words : The sentences contained within a cstring.
number of words : The number of words in the string.
OUTPUT
average letters : The average number of letters per word
_______________________________________________________________________________
*******************************************************************************
*/
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
//Function that calculates the average number of letters per word
int counter (char sentence[])
{
int numLetters; //INPUT The number of letters in the string
int numWords; //INPUT The number of words in the string
int avg; //OUTPUT The average number of letters in the string
//Calculate the number of words and letters in the string
for(int i = 0; i < 100; i++)
{
if(isalpha(sentence[i]))
{
numLetters++;
}
if(isspace(sentence[i]))
{
numWords++;
}
}
numWords++;
//Calculate the average letters per word
avg = numLetters / numWords;
//return the average letters per word
return avg;
}
int main() {
char words[100]; //INPUT The sentences contained within a cstring
//Ask the user for a string of words
cout << "Please enter a string: " << endl;
cin >> words;
//Output the average letters per word
cout << "The average number of letters per word is: " << counter(words) << endl;
return 0;
}