Please help me understand the question and also explain the code, I need it to b
ID: 3766089 • Letter: P
Question
Please help me understand the question and also explain the code, I need it to be in C++
Problem Statement
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
#include "justify.h"
unsigned int justify(unsigned int width, string in, vector<string> & out,int &temp) {
//declaring variables
std::size_t prev = 0, pos;
//parsing for each word
while ((pos = in.find_first_of(" ';", prev)) != std::string::npos)
{
//storing words into vector
if (pos > prev)
out.push_back(in.substr(prev, pos-prev));
prev = pos+1;
}
//if any words left finally we are pushing into vector again
if (prev < in.length())
out.push_back(in.substr(prev, std::string::npos));
//I intriduced extra temp ..because when you are printing text result..after every temp words( for example every 3rd word) print
//new line character. so we can print in required width.
temp = out.size()/width;
return 1;
} // end function justify