|
To get a sense of the problem, use a concrete example:
|
S = number of trailing spaces in the line G = number of gaps between the words in the line Distribute S spaces evenly over the G gaps (need refining) |
Example:
012345678901234567890123456789
>WE hold these Truths to be
length of the string "WE hold these Truths to be" = 26 characters
|
S = 30 − line.length(); |
|
java.util.StringTokenizer |
Check out the Java documentation page of the StringTokenizer class: click here
|
How to run the program:
|
|
![]() |
|
|
|
NTrailingSpaces = MAXLINELENGTH - line.length() ; // # trailing spaces StringTokenizer T = new StringTokenizer( line ); // Break "line" into tokens NGaps = T.countTokens() - 1; // # gaps between words NSpacesPerGap = NTrailingSpaces / NGaps; NExtra = NTrailingSpaces % NGaps; /* ----------------------------------------------- Construct the right-adjusted line ----------------------------------------------- */ Add 1 + NSpacesPerGap spaces between 2 words in StringTokenizer T Add ONE MORE space to the first NExtra gaps |
NTrailingSpaces = MAXLINELENGTH - line.length() ; // # trailing spaces StringTokenizer T = new StringTokenizer( line ); // Break "line" into tokens NGaps = T.countTokens() - 1; // # gaps between words NSpacesPerGap = NTrailingSpaces / NGaps; NExtra = NTrailingSpaces % NGaps; /* ----------------------------------------------- Construct the right-adjusted line ----------------------------------------------- */ outputLine = T.nextToken( ); // First word for ( k = 1; k <= NGaps; k++ ) { /* ------------------------------------------- Add 1+NSpacesPerGap spaces to a gap ------------------------------------------- */ for (i = 1; i <= 1+NSpacesPerGap; i++) outputLine = outputLine + " "; /* ------------------------------------------- Add 1 more space to the first NExtra gaps ------------------------------------------- */ if (k <= NExtra) outputLine = outputLine + " "; /* ------------------------------------------- Add the next word... ------------------------------------------- */ outputLine = outputLine + T.nextToken(); } |
Note:
|
public static String fixLine(String s, int maxLineLength) { int NGaps; int NTrailingSpaces; int NSpacesPerGap; // # spaces to insert in each gap int NExtra; // # gaps that gets ONE extra space String outputLine; // Output string... StringTokenizer T; // Help variables... int i, k; /* ------------------------------------------------- Check if line already fits perfectly.... ------------------------------------------------- */ if ( s.length() == maxLineLength ) return(s); // Already right-justified /* -------------------------------------- Compute: NTrailingSpaces and NGaps -------------------------------------- */ NTrailingSpaces = maxLineLength - s.length(); // # trailing spaces T = new StringTokenizer(s); NGaps = T.countTokens() - 1; // # gaps /* ------------------------------------------------- Check if line has > 1 word.... (Lines with 1 word can't be right-adjusted !) ------------------------------------------------- */ if ( NGaps == 0 ) return( s ); // Can't adjust line, return as is... /* ================== The right adjust line algorithm ============== */ NSpacesPerGap = NTrailingSpaces / NGaps; NExtra = NTrailingSpaces % NGaps; /* ------------------------------------- Construct the right-adjusted line ------------------------------------- */ outputLine = T.nextToken(); // Put first word in output for ( k = 1; k <= NGaps; k++ ) { /* ------------------------------------------- Add 1+NSpacesPerGap spaces to a gap ------------------------------------------- */ for (i = 1; i <= 1+NSpacesPerGap; i++) outputLine = outputLine + " "; /* ------------------------------------------- Add 1 more space to the first NExtra gaps ------------------------------------------- */ if (k <= NExtra) outputLine = outputLine + " "; /* ------------------------------------------- Add the next word... ------------------------------------------- */ outputLine = outputLine + T.nextToken(); } return(outputLine); } |
How to run the program:
|
Example:
|