Last active
August 29, 2015 14:16
-
-
Save Jules-Baratoux/ac083052555acabc3aa4 to your computer and use it in GitHub Desktop.
Homework #7 – Fun with Strings and Vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cout; | |
using std::endl; | |
#include <cstdlib> | |
#include "StringUtility.h" | |
using namespace JulesBaratoux; | |
int main(int argc, const char *argv[]) | |
{ | |
StringUtility::tests::join(); | |
StringUtility::tests::reverse(); | |
StringUtility::tests::combine(); | |
StringUtility::tests::leftPad(); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <ostream> | |
using std::ostream; | |
#include <string> | |
using std::string; | |
#include <vector> | |
using std::vector; | |
namespace JulesBaratoux | |
{ | |
static constexpr auto OSTREAM_START = "["; | |
static constexpr auto OSTREAM_END = "]"; | |
static constexpr auto OSTREAM_SEP = ", "; | |
ostream& operator<< (ostream& out, const vector<string>& strings); | |
struct StringUtility | |
{ | |
// Concatenate strings from the strings parameter together placing the | |
// delimiter character in between each pair of strings. The resulting string | |
// is returned. | |
static string join(const vector<string> & strings, char delimiter); | |
// Return a vector containing the strings from the strings parameter in | |
// reversed order and with the contents of each string reversed. | |
static vector<string> reverse(const vector<string>& strings); | |
// Returns a vector containing every string from left concatenated with | |
// every string from right. | |
static vector<string> combine(const vector<string>& left, const vector<string>& right); | |
// Returns a vector containing each string from the strings parameter | |
// left-padded with the pad character so that each resulting string has | |
// a length equal to the longest original string. | |
static vector<string> leftPad(const vector<string>& strings, char pad); | |
struct tests | |
{ | |
static void join(); | |
static void reverse(); | |
static void combine(); | |
static void leftPad(); | |
}; | |
friend tests; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment