Advertisement
DragonOsman

String Length

May 10th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. // Osman Zakir
  2. // 5/11/2015
  3. // Programming: Principles and Practice Using C++ Second Edition Chapter 8 Exercises
  4. // This program is supposed to find and print the longest and shortest string in a vector of strings
  5. // Find and print the lexicographically first and last string
  6. // And is also supposed to be able to find and print the lengths of each of the strings inside the vector
  7.  
  8. #include "../custom_std_lib_facilities.h"
  9. #include <algorithm>
  10. #include <string>
  11. #include <vector>
  12. #include <limits>
  13.  
  14. using namespace std;
  15.  
  16. vector<string> get_input();
  17. void print_size(const vector<int>& sizes);
  18. vector<int> string_size(const vector<string>& words);
  19. void print_first_last(vector<string>& words);
  20. string longest_string(const vector<string>& words);
  21. string shortest_string(const vector<string>& words);
  22. void print_longest_shortest(const string& shortest, const string& longest);
  23.  
  24. int main()
  25. {
  26.     vector<string> words = get_input();
  27.     vector<int> string_length = string_size(words);
  28.     string shortest = shortest_string(words);
  29.     cout << "\n";
  30.     string longest = longest_string(words);
  31.     cout << "\n";
  32.     print_longest_shortest(shortest, longest);
  33.     cout << "\n";
  34.     print_first_last(words);
  35.     cout << "\n";
  36.     print_size(string_length);
  37.     cout << "\n";
  38.     keep_window_open();
  39. }
  40.  
  41. vector<string> get_input()
  42. {
  43.     string word;
  44.     vector<string> words;
  45.     cout << "Enter some words; enter CTRL+Z to stop.\n";
  46.     while (cin >> word)
  47.     {
  48.         words.push_back(word);
  49.     }
  50.     return words;
  51. }
  52.  
  53. vector<int> string_size(const vector<string>& words)
  54. {
  55.     vector<int> sizes;
  56.     for (auto const& x : words)
  57.     {
  58.         sizes.push_back(x.length());
  59.     }
  60.     return sizes;
  61. }
  62.  
  63. void print_size(const vector<int>& sizes)
  64. {
  65.     cout << "The sizes are:\n";
  66.     for (auto const& x : sizes)
  67.     {
  68.         cout << x << "\n";
  69.     }
  70. }
  71.  
  72. string shortest_string(const vector<string>& words)
  73. {
  74.     string shortest = words[0];
  75.     for (auto const& x : words)
  76.     {
  77.         if (x.length() < shortest.length())
  78.         {
  79.             shortest = x;
  80.         }
  81.     }
  82.     return shortest;
  83. }
  84.  
  85. string longest_string(const vector<string>& words)
  86. {
  87.     string longest = std::numeric_limits<string>::lowest();
  88.     for (auto const& x : words)
  89.     {
  90.         if (x.length() > longest.length())
  91.         {
  92.             longest = x;
  93.         }
  94.     }
  95.     return longest;
  96. }
  97.  
  98. void print_longest_shortest(const string& shortest, const string& longest)
  99. {
  100.     cout << "The longest word is: \'" << longest << "\' and the shortest word is: \'" << shortest << "\'\n";
  101. }
  102.  
  103. void print_first_last(vector<string>& words)
  104. {
  105.     sort(words.begin(), words.end());
  106.     cout << "Lexicographically, the first element is: \'" << words.front() << "\' and the last one is: \'" << words.back() << "\'\n";
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement