Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string leftAlign(const vector<string>& paragraph, int width) {
- string result;
- int len = 0;
- for (const string& word : paragraph) {
- if (len + word.length() <= width) {
- if (len > 0) result += " ";
- result += word;
- len += word.length() + 1;
- }
- }
- result += string(width - len + 1, ' ');
- return result;
- }
- string rightAlign(const vector<string>& paragraph, int width) {
- string result;
- int len = 0;
- for (const string& word : paragraph) {
- if (len + word.length() <= width) {
- if (len > 0) result += " ";
- result += word;
- len += word.length() + 1;
- }
- }
- int spacePadding = width - (len - 1);
- result = string(spacePadding, ' ') + result;
- return result;
- }
- string centerAlign(const vector<string>& paragraph, int width) {
- string result;
- int len = 0;
- for (const string& word : paragraph) {
- if (len + word.length() <= width) {
- if (len > 0) result += " ";
- result += word;
- len += word.length() + 1;
- }
- }
- int totalSpaces = width - (len - 1);
- int leftPadding = totalSpaces / 2;
- int rightPadding = totalSpaces - leftPadding;
- result = string(leftPadding, ' ') + result + string(rightPadding, ' ');
- return result;
- }
- void formatPage(const vector<vector<string>>& paragraphs, const vector<string>& aligns, int width) {
- string border = string(width + 2, '*');
- cout << border << endl;
- for (size_t i = 0; i < paragraphs.size(); ++i) {
- string formattedLine;
- if (aligns[i] == "LEFT") {
- formattedLine = leftAlign(paragraphs[i], width);
- } else if (aligns[i] == "RIGHT") {
- formattedLine = rightAlign(paragraphs[i], width);
- } else if (aligns[i] == "CENTER") {
- formattedLine = centerAlign(paragraphs[i], width);
- }
- cout << "*" << formattedLine << "*" << endl;
- }
- cout << border << endl;
- }
- int main() {
- vector<vector<string>> paragraphs = {
- {"hello", "world"},
- {"How", "areYou", "doing"},
- {"Please look", "and align", "to right"}
- };
- vector<string> aligns = {"LEFT", "RIGHT", "CENTER"};
- int width = 16;
- formatPage(paragraphs, aligns, width);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement