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

Write a program that calculates the average word length of a sentence. Your prog

ID: 3668878 • Letter: W

Question

Write a program that calculates the average word length of a sentence.

Your program should start by asking the user for a sentence. Then it should calculate the average word length of the sentence. Your program should consider punctuation as part of the word to which it is attached. The output should be displayed to one decimal place.

Hints: For this program you’ll need a loop, inside of which there should be 2 or 3 IF/ELSE IF statements. You should also use the following functions:

getchar(): Pulls the next character from the CIN input buffer for analysis. An example can be found on tutorialspoint.com (link below).

Must use #include <stdio.h> at the top of your program.

Explanation / Answer

#include #include int main() { char ch,temp; float avg; int space = 1,alphbt = 0,k = 0; printf("Enter a sentence: "); while((ch = getchar()) != ' ') { temp = ch; if( ch != ' ') { alphbt++; k++; } else if(ch == ' ' && k != 0) space++; } if (temp == ' ') printf("Average word lenth: %.1f",avg = (float) alphbt/(space-1)); else printf("Average word lenth: %.1f",avg = (float) alphbt/space); getch(); }
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