summaryrefslogtreecommitdiff
path: root/CPP/cpp_book/chap6/split_str
diff options
context:
space:
mode:
Diffstat (limited to 'CPP/cpp_book/chap6/split_str')
-rwxr-xr-xCPP/cpp_book/chap6/split_str/split_strbin0 -> 66576 bytes
-rw-r--r--CPP/cpp_book/chap6/split_str/split_str.cpp152
-rw-r--r--CPP/cpp_book/chap6/split_str/text.txt1
3 files changed, 153 insertions, 0 deletions
diff --git a/CPP/cpp_book/chap6/split_str/split_str b/CPP/cpp_book/chap6/split_str/split_str
new file mode 100755
index 0000000..ca1e6aa
--- /dev/null
+++ b/CPP/cpp_book/chap6/split_str/split_str
Binary files differ
diff --git a/CPP/cpp_book/chap6/split_str/split_str.cpp b/CPP/cpp_book/chap6/split_str/split_str.cpp
new file mode 100644
index 0000000..1c03bb2
--- /dev/null
+++ b/CPP/cpp_book/chap6/split_str/split_str.cpp
@@ -0,0 +1,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;
+}
diff --git a/CPP/cpp_book/chap6/split_str/text.txt b/CPP/cpp_book/chap6/split_str/text.txt
new file mode 100644
index 0000000..a6436ff
--- /dev/null
+++ b/CPP/cpp_book/chap6/split_str/text.txt
@@ -0,0 +1 @@
+seu corno puto manso