Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE USE C++ LANGUAGE AND PLEASE TRY TO USE VECTORS, ONLY WRITE CODES IN THE J

ID: 3766299 • Letter: P

Question

PLEASE USE C++ LANGUAGE AND PLEASE TRY TO USE VECTORS, ONLY WRITE CODES IN THE JUSTIFY.CPP NOTHING ELSE, PLEASE HELP URGENT MATTER

THE PROBLEM IS:

Implement the function justify in the file justify.cpp to perform left and right justification on a text input of type string. The justified text is a vector of strings.

The assumptions for the input text are as follows:

1. The input text consists only of alphabets (lower and upper case), blank spaces (‘ ‘), and two punctuation marks (periods (‘.’), and commas (‘,’)).

2. Every word in a sentence in the input text is separated by a single blank space.

3. The punctuation marks immediately follow the last character of the preceding word, and a single space is present between a punctuation mark and the following word.

4. There are no new line characters in the input string.

We will perform a very primitive form of justification in this assignment using the following rules:

1. Every line in the justified text must have at least width – 2 characters, with the exception of the last line, which may have fewer characters.

2. Every line in the justified text must have at most width characters, with the exception of lines that terminate with a punctuation mark (period or comma). In such cases, the line may have width+1 characters to accommodate the trailing punctuation mark.

3. Every line in the justified text must start with an alphabet (word). The first character in a line cannot be a punctuation mark or space.

4. If a word needs to be split across lines in order to follow the above rules, a hyphen (‘-‘) character must be inserted after the first part of the word. The hyphen character counts toward the total number of characters in a line.

Given an example unjustified input text as follows, the program should generate justified texts for different column widths as illustrated

Input text (unjustified)

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non culpa qui officia deserunt mollit anim id est laborum.

Output text (justified) with width = 20

Basically if the justify.cpp is right, im asked to enter the width ( lets say 20) and a random text, then i should be given the above. Each row has 20 characters( as width is chosen as 20).

Explanation / Answer

main.cpp

#include "justify.h"


int main() {
unsigned int result;
unsigned int width;
string input;
vector<string> output;

while (1) {
    // first clear the output string
    output.clear();
    cout << endl<< "Welcome to pretty printing!" << endl << endl;
    cout << "Enter the column width (between " << MIN_COL_WIDTH << " and " <<     MAX_COL_WIDTH << ", or 0 to exit): ";
    cin >> width;
    if (width == 0)
        break;
    cout << "Enter the string to justify below:" << endl << endl;

    // read in the input string
    cin.ignore();

    getline(cin, input);

    // justify the input for given column width
    result = justify(width, input, output);

    switch(result) {
        case ERROR_NONE:
            cout << "The justified string is:" << endl << endl;
            for (unsigned int i = 0; i < output.size(); i++)
                cout << output[i] << endl;
            break;
        case ERROR_NO_CHARS:
            cout << "ERROR: No characters in input text!" << endl;
            break;
        case ERROR_ILLEGAL_WIDTH:
            cout << "ERROR: Column width out of range!" << endl;
            break;
        default:
            cout << "ERROR: Internal error!" << endl;
            break;
    }
    } // enter interactive loop
}

justify.h

#include <iostream>
#include <string>
#include <vector>
#include "ctype.h"

using namespace std;

#define MIN_COL_WIDTH 10
#define MAX_COL_WIDTH 60

// error codes
#define ERROR_NONE 0
#define ERROR_NO_CHARS 1
#define ERROR_ILLEGAL_WIDTH 2

// function prototype
unsigned int justify(unsigned int width, string in, vector<string> &out);

#include "justify.h"

unsigned int justify(unsigned int width, string in, vector<string> & out)
{


while (width < 10 || width >60)
    return 2;

if (in == ""){
    return 1;
}
else

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote