Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::cout;
- using std::endl;
- using std::ostream;
- #include <string>
- using std::string;
- #include <vector>
- using std::vector;
- #include <algorithm>
- #include <cassert>
- #include "StringUtility.h"
- using namespace JulesBaratoux;
- // ----------------------------------------------------------------------------
- ostream& JulesBaratoux::operator<< (ostream& out, const vector<string>& strings)
- {
- vector<string>::size_type i = 0;
- const auto max = strings.size() - 1;
- out << OSTREAM_START;
- while (i < max)
- {
- out << strings[i] << OSTREAM_SEP;
- ++i;
- }
- return out << strings[i] << OSTREAM_END;
- }
- // ----------------------------------------------------------------------------
- string StringUtility::join(const vector<string> & strings, char delimiter)
- {
- const auto end = strings.cend() - 1;
- auto it = strings.cbegin();
- string result;
- while (it != end)
- {
- result += *it + delimiter;
- ++it;
- }
- result += *it;
- return result;
- }
- void StringUtility::tests::join()
- {
- // Example
- const string expected = "abc*def*ghi";
- assert(StringUtility::join({"abc", "def", "ghi"}, '*') == expected);
- // Question 6
- cout << StringUtility::join({"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}, ',') << endl;
- }
- // ----------------------------------------------------------------------------
- vector<string> StringUtility::reverse(const vector<string>& strings)
- {
- auto reversed(strings);
- std::reverse(reversed.begin(), reversed.end());
- for (auto& str : reversed)
- {
- std::reverse(str.begin(), str.end());
- }
- return reversed;
- }
- void StringUtility::tests::reverse()
- {
- // Example
- const vector<string> expected{{"ihg", "fed", "cba"}};
- assert(StringUtility::reverse( {"abc", "def", "ghi"} ) == expected);
- // Question 7
- cout << StringUtility::reverse( {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"} ) << endl;
- }
- // ----------------------------------------------------------------------------
- vector<string> StringUtility::combine(const vector<string>& left, const vector<string>& right)
- {
- vector<string> combined;
- combined.reserve(left.size() * right.size());
- for (const auto& left_string : left)
- {
- for (const auto& right_string : right)
- {
- combined.push_back(left_string + right_string);
- }
- }
- return combined;
- }
- void StringUtility::tests::combine()
- {
- // Example
- const vector<string> left = {"ab", "cd", "ef"};
- const vector<string> right = {"gh", "ij", "kl"};
- const vector<string> expected = {"abgh", "abij", "abkl",
- "cdgh", "cdij", "cdkl",
- "efgh", "efij", "efkl"};
- assert(StringUtility::combine(left, right) == expected);
- // Question 8
- cout << StringUtility::combine({"Mr.", "Mrs."}, {"Jones", "Smith", "Williams"}) << endl;
- }
- // ----------------------------------------------------------------------------
- vector<string> StringUtility::leftPad(const vector<string>& strings, char pad)
- {
- string::size_type size = 0;
- vector<string> padded(strings);
- for (const auto& str : strings)
- {
- if (str.size() > size)
- {
- size = str.size();
- }
- }
- for (auto& str : padded)
- {
- str.insert(0, size - str.size(), pad);
- }
- return padded;
- }
- void StringUtility::tests::leftPad()
- {
- // Example
- const vector<string> expected {{"**a", "*bb", "ccc"}};
- assert(StringUtility::leftPad({"a", "bb", "ccc"}, '*') == expected);
- // Question 9
- cout << StringUtility::leftPad({"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}, '*') << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement