# Program to justify a set of words by converting them into same length lines in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose we have an list of words and a width k, we have to arrange the text such that each line has exactly k number of characters and the text is fully justified. Here we shall pack our words as many words as we can insert in each line. And we shall pad extra spaces ' ' when necessary so that each line has exactly k characters.

Here 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, empty slots on the left will be assigned more spaces than the slots on the right. For the final line of text, it should be left justified and no extra space is inserted between words.

So if the input is like ["The", "grumpy", "wizards", "make", "toxic", "brew", "for", "the", "evil", "queen", "and", "Jack"], and k = 13

then the output will be −

The grumpy
wizards make
toxic brew
for the evil
queen and
Jack

To solve this, we will follow these steps −

• create one array called result
• for i in range 0 to size of a, update i by j
• width := 0
• for j in range i to size of a and width + size of a[j] + j – i <= b,
• width := width + size of a[j]
• space := 1, extra := 0
• if j – 1 != 1 and j != size of a, then
• space := (b - width) / j – i – 1
• extra := (b - width) mod (j – i – 1)
• line := a[i]
• for k in range i + 1 to j
• concatenate space number of blank-spaces with line
• if extra > 0, then concatenate space with line
• decrease extra by 1
• line := concatenate a[k] with line
• x := size of line
• line := concatenate (b - x) number of spaces with line
• insert line into result
• return res

## Example (C++)

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << v[i] << ", ";
}
cout << "]"<<endl;
}
void print_vector(vector<vector<auto> > v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << "[";
for(int j = 0; j <v[i].size(); j++){
cout << v[i][j] << ", ";
}
cout << "],";
}
cout << "]"<<endl;
}
class Solution {
public:
vector<string> fullJustify(vector<string> &a, int b) {
vector <string> result;
int i, j;
for(i = 0; i < a.size(); i = j){
int width = 0;
for(j = i; j < a.size() && width + a[j].size() + j - i <= b; j++){
width += a[j].size();
}
int space = 1;
int extra = 0;
if(j - i != 1 && j != a.size()){
space = (b - width) / (j - i - 1);
extra = (b - width) % (j - i - 1);
}
string line(a[i]);
for(int k = i + 1; k < j; k++){
line += string(space, ' ');
if(extra-- > 0){
line += " ";
}
line += a[k];
}
int x = line.size();
line += string(b - x, ' ');
result.push_back(line);
}
return result;
}
};
main(){
vector<string> v = {"The", "grumpy", "wizards", "make", "toxic", "brew", "for", "the", "evil", "queen", "and", "Jack"};
Solution ob;
print_vector(ob.fullJustify(v, 13));
}

## Input

["I", "love", "coding.", "here", "we", "will", "write", "some", "program"]
16

## Output

[The grumpy,
wizards make,
toxic brew,
for the evil,
queen and,
Jack ,]
Updated on 12-Dec-2020 10:00:33