41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#include <cstdio>
|
|
#include <string>
|
|
#include <cmath>
|
|
|
|
std::string level1BreakIntoLines(std::string text, int width) {
|
|
std::string str{};
|
|
std::size_t col_count = 0;
|
|
std::size_t space_index_text = 0;
|
|
std::size_t space_index = 0;
|
|
for (std::size_t i = 0; i < text.size(); ++i) {
|
|
if (text[i] == ' ') {
|
|
space_index_text = i;
|
|
space_index = str.size();
|
|
}
|
|
str += text[i];
|
|
if (col_count++ >= static_cast<std::size_t>(width)) {
|
|
i = space_index_text;
|
|
str.erase(space_index);
|
|
col_count = 0;
|
|
str += "\n";
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
std::string level2BreakIntoLines(std::string text, int numLines) {
|
|
std::size_t const max_column = std::size_t(std::ceil(double(text.size()) / double(numLines)));
|
|
return level1BreakIntoLines(text, max_column);
|
|
}
|
|
|
|
auto main() -> int {
|
|
auto str = level1BreakIntoLines("Hello, World! This is a very goodtest and we are not here to try it out.", 32);
|
|
std::printf("%s\n", str.c_str());
|
|
|
|
std::printf("\n");
|
|
|
|
auto str1 = level2BreakIntoLines("The quick brown fox jumps over the lazy dog", 3);
|
|
std::printf("%s\n", str1.c_str());
|
|
return 0;
|
|
}
|