53 lines
1.9 KiB
Plaintext
53 lines
1.9 KiB
Plaintext
$ include "seed7_05.s7i";
|
|
|
|
const array string: inputLines is [] (
|
|
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
|
|
"are$delineated$by$a$single$'dollar'$character,$write$a$program",
|
|
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$",
|
|
"column$are$separated$by$at$least$one$space.",
|
|
"Further,$allow$for$each$word$in$a$column$to$be$either$left$",
|
|
"justified,$right$justified,$or$center$justified$within$its$column.");
|
|
|
|
const func array integer: computeColumnWidths (in array string: inputLines) is func
|
|
result
|
|
var array integer: columnWidths is 0 times 0;
|
|
local
|
|
var string: line is "";
|
|
var array string: lineFields is 0 times "";
|
|
var integer: index is 0;
|
|
begin
|
|
for line range inputLines do
|
|
lineFields := split(line, "$");
|
|
if length(lineFields) > length(columnWidths) then
|
|
columnWidths &:= (length(lineFields) - length(columnWidths)) times 0;
|
|
end if;
|
|
for index range 1 to length(lineFields) do
|
|
if length(lineFields[index]) > columnWidths[index] then
|
|
columnWidths[index] := length(lineFields[index]);
|
|
end if;
|
|
end for;
|
|
end for;
|
|
end func;
|
|
|
|
const func string: center (in string: stri, in integer: length) is
|
|
return ("" lpad (length - length(stri)) div 2 <& stri) rpad length;
|
|
|
|
const proc: main is func
|
|
local
|
|
var array integer: columnWidths is 0 times 0;
|
|
var string: line is "";
|
|
var array string: lineFields is 0 times "";
|
|
var integer: index is 0;
|
|
begin
|
|
columnWidths := computeColumnWidths(inputLines);
|
|
for line range inputLines do
|
|
lineFields := split(line, "$");
|
|
for index range 1 to length(lineFields) do
|
|
# write(lineFields[index] rpad columnWidths[index] <& " "); # Left justify
|
|
# write(lineFields[index] lpad columnWidths[index] <& " "); # Right justify
|
|
write(center(lineFields[index], columnWidths[index]) <& " ");
|
|
end for;
|
|
writeln;
|
|
end for;
|
|
end func;
|