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
Select the correct answer.
melomori [17]

A. a next generation car

8 0
3 years ago
What will help the programmer understand what’s going on in the program?
Over [174]

Answer:

Programming languages to help solve algorithms

Explanation:

6 0
3 years ago
How to copy and paste things that won't let you?
VLD [36.1K]
You take the ctrl buutton and hold it down while awlleclting the other setion you wouo would lieke to highlight.
5 0
3 years ago
Naruto Uzumaki who likes naruto ??? who waches it??
nika2105 [10]

Answer:

me

Explanation:

naruto is the coolest dude

6 0
3 years ago
Read 2 more answers
Which of the following emerging technologies uses the principle of chromosomes to identify suspects?
Slav-nsk [51]

Answer:

b

Explanation:

they say this on the quiz but because of the irrationality DNA has

4 0
3 years ago
Other questions:
  • Which of the following refers to special eyeglasses from Google that provide the user with visual information directly in front
    5·1 answer
  • What are some of the good effects of social media
    7·1 answer
  • Where would you go to cancel a print job?
    15·1 answer
  • The statement cout &lt;&lt; sales[0,3] + sales[1,3]; will __________. Use the following array named sales to answer this questio
    14·1 answer
  • If user reading a document on computer, it allows you to navigate on any part of the document called?
    5·1 answer
  • When performing actions between your computer and one that is infected with a virus, which of the following offers NO risk of yo
    11·2 answers
  • What is contrast (in Photography)?
    14·1 answer
  • What is the acronym DOS in full
    15·1 answer
  • artefacto o servicio producido de manera artesanal o Industrial que puede satisfacer plenamente una necesidad o interés de la lo
    8·1 answer
  • Can someone help me to write a python code to save list form python to CSV file please?​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!