1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#include <iostream>
#include <vector>
#include <cctype>
#include <algorithm>
bool
space(char c)
{
return isspace(c);
}
bool
not_space(char c)
{
return !isspace(c);
}
std::vector<std::string>
split(const std::string& s)
{
std::vector<std::string> 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<std::string>& v)
{
std::string::size_type ret = 0;
for (std::vector<std::string>::const_iterator i = v.begin();
i != v.end();
i++)
ret = std::max(ret, i->size());
return ret;
}
std::vector<std::string>
frame(const std::vector<std::string>&v) {
std::vector<std::string> 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<std::string>
vcat(
const std::vector<std::string>& v1,
const std::vector<std::string>& v2) {
std::vector<std::string> ret = v1;
for (std::vector<std::string>::const_iterator i = v2.begin();
i < v2.end();
i++)
ret.push_back(*i);
return ret;
}
std::vector<std::string>
hcat(
const std::vector<std::string>& v1,
const std::vector<std::string>& v2) {
std::vector<std::string> ret;
std::string::size_type w1 = width(v1) + 1;
std::vector<std::string>::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<std::string> vec = split(s);
vec = frame(vec);
for (std::vector<std::string>::const_iterator i = vec.begin();
i < vec.end();
i++) {
std::cout << *i << std::endl;
}
}
std::string a = "Vai curintia eh nois";
std::vector<std::string> v1 = split(a);
v1 = frame(v1);
std::string b = "Another string test ronaldo";
std::vector<std::string> v2 = split(b);
std::vector<std::string> v3 = hcat(v1, v2);
for (std::vector<std::string>::const_iterator i = v3.begin();
i < v3.end();
i++) {
std::cout << *i << std::endl;
}
return 0;
}
|