Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void){
  4.  
  5. /*
  6. Write the program that calculates the average word length for
  7. a sentence :
  8.  
  9. Average word length: 3.4
  10.  
  11. */
  12.  
  13. int wordLengthCounter = 0, numWordsCounter = 0, sumChars = 0;
  14. float averageWordLength;
  15. char c;
  16.  
  17. printf("\nEnter a sentence: ");
  18.  
  19. // Enter a sentence:
  20.  
  21. // It was deja all over again.
  22.  
  23. while((c = getchar()) != '\n'){
  24. while(c != ' '){
  25. wordLengthCounter++;
  26. }
  27. sumChars += wordLengthCounter;
  28.  
  29. if(wordLengthCounter > 0) numWordsCounter++;
  30.  
  31. wordLengthCounter = 0;
  32. }
  33.  
  34. averageWordLength = (float)sumChars / (float)numWordsCounter;
  35.  
  36. printf("\nAverage word length: %.1f\n", averageWordLength);
  37.  
  38. return(0);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement