Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- // Function to left-align a paragraph
- 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, ' '); // Fill remaining space
- return result;
- }
- // Function to right-align a paragraph
- 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); // Calculate remaining space
- result = string(spacePadding, ' ') + result;
- return result;
- }
- // Function to center-align a paragraph
- 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;
- }
- // Function to format the entire page with borders
- void formatPage(const vector<vector<string>>& paragraphs, const vector<string>& aligns, int width) {
- string border = string(width + 2, '*'); // Create top and bottom border
- cout << border << endl; // Print top border
- 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; // Add left and right borders
- }
- cout << border << endl; // Print bottom border
- }
- int main() {
- vector<vector<string>> paragraphs = {
- {"hello", "world"},
- {"How", "areYou", "doing"},
- {"Please look", "and align", "to right"}
- };
- vector<string> aligns = {"LEFT", "RIGHT", "RIGHT"};
- int width = 16;
- formatPage(paragraphs, aligns, width);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement