fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int MAX_SIZE = 100;
  7.  
  8. bool isLetter(char c) {
  9. return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
  10. }
  11.  
  12. double mediumWordsLength(char sentence[]) {
  13. int n = strlen(sentence), isWord = 0, wordsCount = 0, lettersCount = 0;
  14. for (int i = 0; i <= n; ++i) {
  15. if (isLetter(sentence[i])) {
  16. isWord = 1;
  17. ++lettersCount;
  18. } else if (isWord) {
  19. ++wordsCount;
  20. isWord = 0;
  21. }
  22. }
  23. if (lettersCount > 0) {
  24. return double(lettersCount) / wordsCount;
  25. }
  26. return 0;
  27. }
  28.  
  29. int main() {
  30. char sentence[MAX_SIZE + 1];
  31. cin.getline(sentence, MAX_SIZE + 1);
  32. cout << fixed << setprecision(2) << mediumWordsLength(sentence);
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5284KB
stdin
ana, are, mere
stdout
3.33