fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main() {
  6. string word;
  7.  
  8. cout << "Type a text:" << endl;
  9.  
  10. if (cin >> word) {
  11. size_t count_words = 1;
  12. string longest_word = word, shortest_word = word;
  13.  
  14. while (cin >> word) {
  15. ++count_words;
  16. if (word.size() > longest_word.size())
  17. longest_word = word;
  18. if (word.size() < shortest_word.size())
  19. shortest_word = word;
  20. }
  21.  
  22. cout << "The text contains " << count_words << " word(s)." << endl;
  23. cout << "The shortest word is " << shortest_word << "." << endl;
  24. cout << "It has " << shortest_word.size() << " character(s)." << endl;
  25. cout << "The longest word is " << longest_word << "." << endl;
  26. cout << "It has " << longest_word.size() << " character(s)." << endl;
  27. }
  28. else {
  29. cout << "No text was entered." << endl;
  30. }
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5460KB
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).