Azienda coinvolta
Given a single-line text string and a maximum width value, write the function 'string justify(string text, int maxWidth)' that formats the input text using full-justification, i.e., extra spaces on each line are equally distributed between the words; the first word on each line is flushed left and the last word on each line is flushed right.
Anonimo
public void justify(String input, int lineLength) { String inputNoSpace = input.replace(" ", ""); int actualLength = inputNoSpace.length(); StringTokenizer stk = new StringTokenizer(input); int tknCount = stk.countTokens(); int emptySpace = lineLength - actualLength; if (emptySpace > tknCount) { int space = emptySpace / (tknCount - 1); System.out.println(tknCount); while (stk.hasMoreElements()) { System.out.print(stk.nextElement()); if (emptySpace / (tknCount - 1) > 0) { for (int i = 0; i < space; i++) { System.out.print(" "); emptySpace--; } } if (emptySpace / (tknCount - 1) == 0) { int remainingSpace=emptySpace; for (int j = 0; j < remainingSpace; j++) { System.out.print(" "); emptySpace--; } } } } }