
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to justify a set of words by converting them into same length lines in C++
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 −
#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 ,]
- Related Articles
- C# program to join words into a string in C#
- C program to count characters, lines and number of words in a file
- Program to reverse words separated by set of delimiters in python
- Program to find length of longest contiguous sublist with same first letter words in Python
- C++ Program for converting hours into minutes and seconds
- Python program to print even length words in a string
- How to sum time in MySQL by converting into seconds?
- Program to equal two strings of same length by swapping characters in Python
- Set the limit of text length to N lines using CSS
- Java Program to Print even length words
- Program to find maximum length of non-sharing words in Python
- Python program to remove K length words in String
- Arranging words by their length in a sentence in JavaScript
- C program to write all digits into words using for loop
- Python program to count distinct words and count frequency of them
