Untitled
unknown
plain_text
2 years ago
3.0 kB
1
Indexable
Never
#include <bits/stdc++.h> using namespace std; class Alignment { private: string _text; int _wordPerLine; public: Alignment(string &text, int &wordPerLine) { printf("Enter Your Text: "); getline(cin, text); printf("Word per line: "); scanf("%d", &wordPerLine); _text = text; _wordPerLine = wordPerLine; } }; void vector_indexing(string text, int wordPerLine, vector<pair<int, int> > &lineIndex) { int startIndex, endIndex, tempEndIndex, nextStartIndex, flag=0; int textLength = text.length(); for(int i=0; i<textLength; i++) { if(text[i]==' ') { //head whitespace checking continue; } startIndex = i; tempEndIndex = startIndex+wordPerLine-1; if(textLength-1<=tempEndIndex) { endIndex = textLength-1; lineIndex.push_back({startIndex, endIndex}); flag = 1; } break; } while(tempEndIndex<textLength && flag!=1) { if(text[tempEndIndex+1]==' ') { endIndex = tempEndIndex; nextStartIndex = tempEndIndex+2; } else { for(int i=tempEndIndex;;i--) { if(text[i]==' ') { endIndex = i-1; nextStartIndex = i+1; break; } } } lineIndex.push_back({startIndex, endIndex}); startIndex = nextStartIndex; tempEndIndex = startIndex+wordPerLine-1; if(tempEndIndex>=textLength-1) { endIndex=textLength-1; lineIndex.push_back({startIndex, endIndex}); break; } } } void left_alignment(string text, vector<pair<int, int> > &lineIndex) { printf("\nText on Left Alignment: \n\n"); for(int i=0; i<lineIndex.size(); i++) { for(int j=lineIndex[i].first; j<=lineIndex[i].second; j++) { printf("%c", text[j]); } printf("\n"); } } void right_alignment(string text, int wordPerLine, vector<pair<int, int> > &lineIndex) { printf("\nText on Right Alignment: \n\n"); for(int i=0; i<lineIndex.size(); i++) { int spaceNumber = wordPerLine - (lineIndex[i].second - lineIndex[i].first + 1); for(int j=1; j<=spaceNumber; j++) { printf(" "); } for(int j=lineIndex[i].first; j<=lineIndex[i].second; j++) { printf("%c", text[j]); } printf("\n"); } } int main() { string text; int wordPerLine; vector<pair<int, int> > lineIndex; Alignment obj1(text, wordPerLine); vector_indexing(text, wordPerLine, lineIndex); left_alignment(text, lineIndex); right_alignment(text, wordPerLine, lineIndex); }