Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::ostream;
  5.  
  6. #include <string>
  7. using std::string;
  8.  
  9. #include <vector>
  10. using std::vector;
  11.  
  12. #include <algorithm>
  13.  
  14. #include <cassert>
  15.  
  16. #include "StringUtility.h"
  17. using namespace JulesBaratoux;
  18.  
  19. // ----------------------------------------------------------------------------
  20. ostream& JulesBaratoux::operator<< (ostream& out, const vector<string>& strings)
  21. {
  22. vector<string>::size_type i = 0;
  23. const auto max = strings.size() - 1;
  24.  
  25. out << OSTREAM_START;
  26. while (i < max)
  27. {
  28. out << strings[i] << OSTREAM_SEP;
  29. ++i;
  30. }
  31. return out << strings[i] << OSTREAM_END;
  32. }
  33.  
  34.  
  35. // ----------------------------------------------------------------------------
  36. string StringUtility::join(const vector<string> & strings, char delimiter)
  37. {
  38. const auto end = strings.cend() - 1;
  39. auto it = strings.cbegin();
  40.  
  41. string result;
  42.  
  43. while (it != end)
  44. {
  45. result += *it + delimiter;
  46. ++it;
  47. }
  48.  
  49. result += *it;
  50. return result;
  51. }
  52.  
  53. void StringUtility::tests::join()
  54. {
  55. // Example
  56. const string expected = "abc*def*ghi";
  57.  
  58. assert(StringUtility::join({"abc", "def", "ghi"}, '*') == expected);
  59.  
  60. // Question 6
  61. cout << StringUtility::join({"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}, ',') << endl;
  62. }
  63.  
  64. // ----------------------------------------------------------------------------
  65. vector<string> StringUtility::reverse(const vector<string>& strings)
  66. {
  67. auto reversed(strings);
  68.  
  69. std::reverse(reversed.begin(), reversed.end());
  70.  
  71. for (auto& str : reversed)
  72. {
  73. std::reverse(str.begin(), str.end());
  74. }
  75.  
  76. return reversed;
  77. }
  78.  
  79. void StringUtility::tests::reverse()
  80. {
  81. // Example
  82. const vector<string> expected{{"ihg", "fed", "cba"}};
  83.  
  84. assert(StringUtility::reverse( {"abc", "def", "ghi"} ) == expected);
  85.  
  86. // Question 7
  87. cout << StringUtility::reverse( {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"} ) << endl;
  88. }
  89.  
  90. // ----------------------------------------------------------------------------
  91. vector<string> StringUtility::combine(const vector<string>& left, const vector<string>& right)
  92. {
  93. vector<string> combined;
  94.  
  95. combined.reserve(left.size() * right.size());
  96.  
  97. for (const auto& left_string : left)
  98. {
  99. for (const auto& right_string : right)
  100. {
  101. combined.push_back(left_string + right_string);
  102. }
  103. }
  104.  
  105. return combined;
  106. }
  107.  
  108. void StringUtility::tests::combine()
  109. {
  110. // Example
  111. const vector<string> left = {"ab", "cd", "ef"};
  112. const vector<string> right = {"gh", "ij", "kl"};
  113. const vector<string> expected = {"abgh", "abij", "abkl",
  114. "cdgh", "cdij", "cdkl",
  115. "efgh", "efij", "efkl"};
  116.  
  117. assert(StringUtility::combine(left, right) == expected);
  118.  
  119. // Question 8
  120. cout << StringUtility::combine({"Mr.", "Mrs."}, {"Jones", "Smith", "Williams"}) << endl;
  121. }
  122.  
  123. // ----------------------------------------------------------------------------
  124. vector<string> StringUtility::leftPad(const vector<string>& strings, char pad)
  125. {
  126. string::size_type size = 0;
  127. vector<string> padded(strings);
  128.  
  129. for (const auto& str : strings)
  130. {
  131. if (str.size() > size)
  132. {
  133. size = str.size();
  134. }
  135. }
  136.  
  137. for (auto& str : padded)
  138. {
  139. str.insert(0, size - str.size(), pad);
  140. }
  141.  
  142. return padded;
  143. }
  144.  
  145. void StringUtility::tests::leftPad()
  146. {
  147. // Example
  148. const vector<string> expected {{"**a", "*bb", "ccc"}};
  149.  
  150. assert(StringUtility::leftPad({"a", "bb", "ccc"}, '*') == expected);
  151.  
  152. // Question 9
  153. cout << StringUtility::leftPad({"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}, '*') << endl;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement