1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
irakobra [83]
3 years ago
12

We are building a word processor and we would like to implement a "reflow" functionality that full-justifies text.Given a maximu

m line width and an array containing lines of text, re-flow the text to fit the new width. Each line should be have the exact specified width. If any line is too short, insert spaces between words as equally as possible until it fits.
Computers and Technology
1 answer:
adoni [48]3 years ago
6 0

Answer:

The code is given below in Java with appropriate comments

Explanation:

Reflow is an function which helps you to justify your functionality

You need to input a string array and the max width and you will get the

output in a list(string).

public List<String> Reflow(String[] input, int width) {

   List<String> result = new ArrayList<String>();

 

   if(input==null || input.length==0){

       return result;

   }

 

 

   int count=0;

   int last=0;

   ArrayList<String> list = new ArrayList<String>();

   for(int i=0; i<input.length; i++){

       count = count + input[i].length();

 

       if(count+i-last>width){

           int wl = count-input[i].length();

           int sl = maxWidth-wl;

           int each = 1;

           int el = 0;

 

           if(i-last-1>0){

               each = sl/(i-last-1);

               el = sl%(i-last-1);

           }

 

           StringBuilder sb = new StringBuilder();

 

           for(int k=last; k<i-1; k++){

               sb.append(input[k]);

 

               int ce = 0;

               while(ce<each){

                   sb.append(" ");

                   ce++;

               }

 

               if(el>0){

                   sb.append(" ");

                   el--;

               }

           }

 

           sb.append(input[i-1]);//last input in the line

           //if only one word in this line, need to fill left with space

           while(sb.length()<width){

               sb.append(" ");

           }

 

           result.add(sb.toString());

 

           last = i;

           count=input[i].length();

       }

   }

 

   int lastLen = 0;

   StringBuilder sb = new StringBuilder();

 

   for(int i=last; i<input.length-1; i++){

       count = count+input[i].length();

       sb.append(input[i]+" ");

   }

 

   sb.append(input[input.length-1]);

   int d=0;

   while(sb.length()<maxWidth){

       sb.append(" ");

   }

   result.add(sb.toString());

 

   return result;

}

You might be interested in
Which engineer may design a GPS for a vehicle?
Crank

Explanation:

Mechanical engineer may design a gps for a vehicle.

4 0
3 years ago
Which statement best describes the purpose of SmartArt graphics in PowerPoint presentations?
Alchen [17]

Answer:

They are an easy mechanism for combining text and graphics.

Explanation:

7 0
3 years ago
Read 2 more answers
A hard drive that uses fde is often referred to as a(n) ____.
Sergio039 [100]

FDE stands for full disk encryption. FDE encrypts data as it is written to the disk and decrypt data as it is read off the disk. It is the simplest method of deploying encryption ,transparent to applications, databases, and users.

A hard drive that uses FDE is often referred to as a self-encrypting hard drive. correct answer: B

6 0
3 years ago
Which of the following is an example of a database?
Katyanochek1 [597]
I think it’s A or D hope that helps
6 0
3 years ago
Read 2 more answers
Instructions:Drag the tiles to the boxes to form correct pairs.
nadya68 [22]
#1-2
#2-3
#3-5
#4-1
#5-4
4 0
3 years ago
Read 2 more answers
Other questions:
  • Consider the following method intended to modify the parameter names by removing all instances of the String n.
    14·1 answer
  • Ted is a fashion designer. Where is he most likely to work
    9·1 answer
  • Given: int[][] values = new int[4][5] Using the statement above, write a nested loop to set values as follows: (3 pts) [0] [1] [
    13·1 answer
  • Computers help eliminate that repetitive of manual task. How can this benefit you in in your overall career
    7·2 answers
  • Click/press _______ to remove the most recently typed text.
    12·2 answers
  • Write a 2-to-3-page research paper describing in plain language the basic purpose, theory, and implementation of GUI application
    6·1 answer
  • Which two functions can be used with the math module?
    13·1 answer
  • ____ is a technology that exists inside another device
    9·2 answers
  • Why is a Quality assurance tester needed on a software development team?
    5·1 answer
  • If you click on repeat header rows what will happen?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!