Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void){
- /*
- Write the program that calculates the average word length for
- a sentence :
- Average word length: 3.4
- */
- int wordLengthCounter = 0, numWordsCounter = 0, sumChars = 0;
- float averageWordLength;
- char c;
- printf("\nEnter a sentence: ");
- // Enter a sentence:
- // It was deja all over again.
- while((c = getchar()) != '\n'){
- while(c != ' '){
- wordLengthCounter++;
- }
- sumChars += wordLengthCounter;
- if(wordLengthCounter > 0) numWordsCounter++;
- wordLengthCounter = 0;
- }
- averageWordLength = (float)sumChars / (float)numWordsCounter;
- printf("\nAverage word length: %.1f\n", averageWordLength);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement