PLEASE USE C++ LANGUAGE AND PLEASE TRY TO USE VECTORS, ONLY WRITE CODES IN THE J
ID: 3766430 • Letter: P
Question
PLEASE USE C++ LANGUAGE AND PLEASE TRY TO USE VECTORS, ONLY WRITE CODES IN THE JUSTIFY.CPP , please do not touch or change main.cpp or justify.h and only write the function for justify.cpp. so many people has either changed the main.cpp but thats not the task im asking.
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
#include <cstdlib>
#include <sstream>
//#include <algorithm>
#include <iterator>
#include <cctype>
#include "justify.h"
using namespace std;
unsigned int justify(unsigned int width, string in, vector<string> &out)
{
int flag;
//check for illegal width
if(width < 10 || width > 60)
{
flag=ERROR_ILLEGAL_WIDTH;
}
//check for blank input
if(in.length()==0)
{
flag=ERROR_NO_CHARS;
}
cout<<isalpha(in.at(0))<<endl;
if(isalpha(in.at(0))) //checks if string starts with a alphabet
{
string token;
for(unsigned i=0;i<in.length();i+=width)
{
if((in.length()-i) < width)
token=in.substr(i,in.length()-i);
else
token=in.substr(i,width);
//checks if token starts with an alphabet
if(isalpha(token.at(0)))
{
out.push_back(token);
}
else if(token.at(0)==' ') //checks is an string starts with space.
{
token.erase(token.begin()+0);
out.push_back(token);
}
}
flag=ERROR_NONE;
}
return flag;
}
Note: This method gives prelimanary justification with evey line of equal width. Needs to be modified for adding remaining rules like addition of '-' etc.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.