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
व्याख्या c) Differentiate between Raster Image and Vector Image.​
crimeas [40]

Vector images are described by lines, shapes, and other graphic image components stored in a format that incorporates geometric formulas for rendering the image elements

These are the types of images that are produced when scanning or photographing an object. Raster images are compiled using pixels, or tiny dots, containing unique color and tonal information that come together to create the image.

7 0
2 years ago
Explain how to number text in a document​
uranmaximum [27]

Answer:

Lol

Explanation:

Yes iri

8 0
3 years ago
Read 2 more answers
1) a program that is designed to perform only one task.
JulijaS [17]

Answer:

special purpose application software

Explanation:

it is a type of software created to execute one specific task.for example a camera application on your phone wul only allow you to take and share pictures.

Other example of special purpose application are web browsers,calculators,media playors,calendar programs e.t.c.

7 0
2 years ago
Write a program that writes 10 random numbers into a file named numbers using loops.
MariettaO [177]

Answer:

The program in cpp for the given scenario is shown below.

#include <stdio.h>

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

   //object created of file stream

   ofstream out;

   //file opened for writing

   out.open("numbers.txt");

   std::cout << "File opened." << std::endl;

   //inside for loop, random numbers written to numbers.txt using rand()

   for(int n=0; n<10; n++)

   {

       out<<rand()<<endl;

   }

   //file closed

   out.close();

   std::cout << "File closed." << std::endl;

   return 0;

}

Explanation:

1. An object of the ofstream is created. The ofstream refers to output file stream. This is used to create file for write and append operations.

ofstream out;

2. The file named, numbers.txt, is opened using the open() method with the object of ofstream.

out.open("numbers.txt");

3. The message is displayed to the user that the file is opened.

4. Inside a for loop, which executes 10 times, a random number is generated. This random number is written to the file and a new line inserted.

5. The random number is generated using rand() method. Every number is written on  new line.

out<<rand()<<endl;

6. Every execution of the for loop repeats steps 4 and 5.

7. When the loop ends, the file is closed using close() method with the object of ofstream.

out.close();

8. A message is displayed to the user that the file is closed.

9. The header file, fstream, is included to support file operations.

10. The extension of the file can be changed from .txt to any other type of file as per requirement.

11. The program can be tested for writing more numbers to the file.

12. The program can be tested for writing any type of numeric data to the file.

13. The program is written in cpp due to simplicity.

14. In other languages such as java or csharp, the code is written inside classes.

15. In cpp, all the code is written inside main() method.

16.    The screenshot of the output messages displayed is attached.

3 0
2 years ago
A hard drive that is running slowly may not have been a0properly.
kipiarov [429]
Its outdated maybe because of time issues or over-usage<span />
6 0
2 years ago
Read 2 more answers
Other questions:
  • All of the Internet in your country disappears. What happens next?
    13·1 answer
  • Write a C++ Programm in which inheritance is used
    7·1 answer
  • Facts and statistics collected together to be used for different purposes is
    14·1 answer
  • How do you find a single number or name you want in a
    7·1 answer
  • Write a flowchart and C code for a program that does the following: Uses a do...while loop. Prints the numbers from 1 to 10 with
    13·1 answer
  • Provide the user with a menu for the following 10 algorithms. Then ask which algorithm the user decides she/he wants to use. Fin
    9·1 answer
  • Which of the following helps you plan out every step of an animation?
    12·2 answers
  • What is not recyclable in a<br> hybrid car<br> hydrogen car<br> petrol car
    15·1 answer
  • What are the advantages of using a vpn?​
    11·1 answer
  • I am trying to sum up a set of data based on 2 criteria (needs to have "green" in column E, and "January 2020" in collum A). Col
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!