fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. size_t count_words = 0;
  7. string word, longest_word, shortest_word;
  8.  
  9. cout << "Type a text:" << endl;
  10.  
  11. while (cin >> word)
  12. {
  13. ++count_words;
  14. if (word.size() > longest_word.size())
  15. longest_word = word;
  16. if (shortest_word.empty() || word.size() < shortest_word.size())
  17. shortest_word = word;
  18. }
  19.  
  20. cout << "The text contains " << count_words << " word(s)." << endl;
  21. if (count_words > 0) {
  22. cout << "The shortest word is " << shortest_word << "." << endl;
  23. cout << "It has " << shortest_word.size() << " character(s)." << endl;
  24. cout << "The longest word is " << longest_word << "." << endl;
  25. cout << "It has " << longest_word.size() << " character(s)." << endl;
  26. }
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5304KB
stdin
The quick brown fox jumps over the lazy dog
stdout
Type a text:
The text contains 9 word(s).
The shortest word is The.
It has 3 character(s).
The longest word is quick.
It has 5 character(s).