#include #include #include #include bool space(char c) { return isspace(c); } bool not_space(char c) { return !isspace(c); } std::vector split(const std::string& s) { std::vector ret; std::string::const_iterator i = s.begin(); /* Invariant: we have processed characters [start i, i) */ while (i != s.end()) { /* Find the first non_space char */ i = find_if(i, s.end(), not_space); /* * Find the end of current word * * Invariant: None of the characters between [i, j) are spaces */ std::string::const_iterator j = find_if(i, s.end(), space); /* If we found non-whitespace chars, push to the vector */ if (i != s.end()) ret.push_back(std::string(i, j)); i = j; } return ret; } std::string::size_type width(const std::vector& v) { std::string::size_type ret = 0; for (std::vector::const_iterator i = v.begin(); i != v.end(); i++) ret = std::max(ret, i->size()); return ret; } std::vector frame(const std::vector&v) { std::vector ret; std::string::size_type w = width(v); std::string border(w + 4, '*'); ret.push_back(border); for (std::string::size_type i = 0; i != v.size(); i++) ret.push_back("* " + v[i] + std::string(w - v[i].size(), ' ') + " *"); ret.push_back(border); return ret; } std::vector vcat( const std::vector& v1, const std::vector& v2) { std::vector ret = v1; for (std::vector::const_iterator i = v2.begin(); i < v2.end(); i++) ret.push_back(*i); return ret; } std::vector hcat( const std::vector& v1, const std::vector& v2) { std::vector ret; std::string::size_type w1 = width(v1) + 1; std::vector::size_type i = 0, j = 0; while (i != v1.size() || j != v2.size()) { std::string s; if (i != v1.size()) s = v1[i++]; s += std::string(w1 - s.size(), ' '); if (j != v2.size()) s += v2[j++]; ret.push_back(s); } return ret; } int main(void) { std::string s; while (getline(std::cin, s)) { std::vector vec = split(s); vec = frame(vec); for (std::vector::const_iterator i = vec.begin(); i < vec.end(); i++) { std::cout << *i << std::endl; } } std::string a = "Vai curintia eh nois"; std::vector v1 = split(a); v1 = frame(v1); std::string b = "Another string test ronaldo"; std::vector v2 = split(b); std::vector v3 = hcat(v1, v2); for (std::vector::const_iterator i = v3.begin(); i < v3.end(); i++) { std::cout << *i << std::endl; } return 0; }