Advertisement
asdfg0998

paragraphAlignment.cpp

Oct 5th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string leftAlign(const vector<string>& paragraph, int width) {
  6.     string result;
  7.     int len = 0;
  8.     for (const string& word : paragraph) {
  9.         if (len + word.length() <= width) {
  10.             if (len > 0) result += " ";
  11.             result += word;
  12.             len += word.length() + 1;
  13.         }
  14.     }
  15.     result += string(width - len + 1, ' ');
  16.     return result;
  17. }
  18.  
  19.  
  20. string rightAlign(const vector<string>& paragraph, int width) {
  21.     string result;
  22.     int len = 0;
  23.     for (const string& word : paragraph) {
  24.         if (len + word.length() <= width) {
  25.             if (len > 0) result += " ";
  26.             result += word;
  27.             len += word.length() + 1;
  28.         }
  29.     }
  30.     int spacePadding = width - (len - 1);
  31.     result = string(spacePadding, ' ') + result;
  32.     return result;
  33. }
  34.  
  35. string centerAlign(const vector<string>& paragraph, int width) {
  36.     string result;
  37.     int len = 0;
  38.     for (const string& word : paragraph) {
  39.         if (len + word.length() <= width) {
  40.             if (len > 0) result += " ";
  41.             result += word;
  42.             len += word.length() + 1;
  43.         }
  44.     }
  45.     int totalSpaces = width - (len - 1);
  46.     int leftPadding = totalSpaces / 2;
  47.     int rightPadding = totalSpaces - leftPadding;
  48.     result = string(leftPadding, ' ') + result + string(rightPadding, ' ');
  49.     return result;
  50. }
  51.  
  52. void formatPage(const vector<vector<string>>& paragraphs, const vector<string>& aligns, int width) {
  53.     string border = string(width + 2, '*');
  54.     cout << border << endl;
  55.  
  56.     for (size_t i = 0; i < paragraphs.size(); ++i) {
  57.         string formattedLine;
  58.         if (aligns[i] == "LEFT") {
  59.             formattedLine = leftAlign(paragraphs[i], width);
  60.         } else if (aligns[i] == "RIGHT") {
  61.             formattedLine = rightAlign(paragraphs[i], width);
  62.         } else if (aligns[i] == "CENTER") {
  63.             formattedLine = centerAlign(paragraphs[i], width);
  64.         }
  65.         cout << "*" << formattedLine << "*" << endl;
  66.     }
  67.  
  68.     cout << border << endl;
  69. }
  70.  
  71. int main() {
  72.     vector<vector<string>> paragraphs = {
  73.         {"hello", "world"},
  74.         {"How", "areYou", "doing"},
  75.         {"Please look", "and align", "to right"}
  76.     };
  77.    
  78.     vector<string> aligns = {"LEFT", "RIGHT", "CENTER"};
  79.     int width = 16;
  80.  
  81.     formatPage(paragraphs, aligns, width);
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement