diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-23 21:51:59 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-23 21:51:59 +0100 |
| commit | 7b7c97be0cba9282c01d6661e0eff394e7c0ebbb (patch) | |
| tree | 4405d2c2ef3332213107e04442bfed7c78de6a80 /challenge-008/paulo-custodio/cpp/ch-2.cpp | |
| parent | 7fed43036d14afef4f61292fa2ce0eeceb9fa9d8 (diff) | |
| download | perlweeklychallenge-club-7b7c97be0cba9282c01d6661e0eff394e7c0ebbb.tar.gz perlweeklychallenge-club-7b7c97be0cba9282c01d6661e0eff394e7c0ebbb.tar.bz2 perlweeklychallenge-club-7b7c97be0cba9282c01d6661e0eff394e7c0ebbb.zip | |
Add C and C++ solutions to challenge 008
Diffstat (limited to 'challenge-008/paulo-custodio/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-008/paulo-custodio/cpp/ch-2.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-008/paulo-custodio/cpp/ch-2.cpp b/challenge-008/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..aeca0d65e4 --- /dev/null +++ b/challenge-008/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,38 @@ +/* +Challenge 008 + +Challenge #2 +Write a function, ‘center’, whose argument is a list of strings, which will +be lines of text. The function should insert spaces at the beginning of the +lines of text so that if they were printed, the text would be centered, and +return the modified lines. +*/ + +#include <iostream> +#include <vector> +#include <string> +using namespace std; + +void center(vector<string>& lines) { + size_t max_len = 0; + + for (auto& line : lines) + if (line.size() > max_len) + max_len = line.size(); + + for (auto& line : lines) + line = string((max_len - static_cast<int>(line.size())) / 2, ' ') + line; +} + +int main(int argc, char* argv[]) { + argc--; argv++; + + vector<string> lines; + for (int i = 0; i < argc; i++) + lines.push_back(argv[i]); + + center(lines); + + for (auto& line : lines) + cout << line << endl; +} |
