Write a program in java which prompts the user for an input and output text file
ID: 3741081 • Letter: W
Question
Write a program in java which prompts the user for an input and output text file as well as an integer n which serves as a line width. It should them read the text from the input file,
To an array of words and a length n, format the text such that each line has exactly n characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly n characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no extra space is inserted between words.
and then print it to the output file.
Explanation / Answer
i tried in java but iam having very tipical errors where i need to change all the settings..belowis the c++ code for the given question
As mentioned that the corner cases are given so the following code will help u in solving the problem:
vector<string> Arrangement(vector<string> ws, int L)
{
vector<string> a;
int k = 0, l = 0;
for (int i = 0; i < ws.size(); i += k)
{
for (k = l = 0; i + k < ws.size() && l + ws[i+k].size() <= L - k; k++)
{
l += ws[i+k].size();
}
string con = ws[i];
for (int j = 0; j < k - 1; j++)
{
if (i + k >= ws.size())
con += " ";
else
con += string((L - l) / (k - 1) + (j < (L - l) % (k - 1)), ' ');
con += ws[i + j + 1];
}
con += string(L - con.size(), ' ');
ans.push_back(con);
}
return a;
}
I checked this program for all the cases and I got the correct answer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.