My final code for your programming delight:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char ch;
char prev_ch='a';
char first_ch;
int counter=0;
float word_counter=0;
float letter_counter=0;
float wpl_counter;
printf("Enter words, hit enter\n and then type ctrl Z\n");
ch=getchar();
first_ch=ch; //checks to see if first ch is a blank space for first and last blank space = a word overcount
while(ch!=EOF)
{
if(counter!=0)
{
ch=getchar();
}
counter++;
if(isspace(ch))
{
if(isspace(prev_ch)&&isspace(ch))
{
continue; // doesn't count two spaces in a row as a new word
}
else if(ch=='\n')
{
continue;
}
word_counter++; //counts total # of words
continue; //skips counting whitespace as number of letters calculated below
}
else if(ispunct(ch))
{
continue; //skips counting puncutation as number of letters calculated below
}
else //ch must be a letter
{
letter_counter++; //adds up total # of letters
}
prev_ch=ch; // stores previous ch to check for two blank spaces in a row
}
if(isspace(first_ch)&&isspace(ch))
{
word_counter--; //corrects for extra word count if the first and last ch are both blank spaces
}
else if(!(isspace(first_ch)&&isspace(ch)))
{
word_counter++; //corrects for lack of counting a word if no space at start or end of input
}
letter_counter--; //corrects for "\n" (return) entered
wpl_counter=letter_counter/word_counter;
printf("Total number of words is %.0f\n", word_counter);
printf("Total number of letters is %.0f\n", letter_counter);
printf("Total number of words per letter is %.2f\n", wpl_counter);
return EXIT_SUCCESS;
}