program adjust (input, output); const DEBUGGING = true; MINLENGTH = 20; MAXLENGTH = 80; BLANK = ' '; type LineType = packed array [1..MAXLENGTH] of char; var line: LineType; desiredLength, actualLength: integer; procedure GetDesired (var desiredLength: integer); begin write ('How long do you want the line to be? '); readln (desiredLength); while not (desiredLength in [MINLENGTH..MAXLENGTH]) do begin writeln ('Sorry, that''s not a legal line length.'); write ('Please type a number between ', MINLENGTH: 1, ' and ', MAXLENGTH: 1, '.'); write ('How long do you want the line to be? '); readln (desiredLength); end; end; procedure GetLine (var line: LineType; var length: integer); var ch: char; k: integer; begin writeln ('Please type a line.'); length := 0; for k := 1 to MAXLENGTH do begin line [k] := BLANK; end; while not eoln do begin read (ch); length := length + 1; if length <= MAXLENGTH then begin line [length] := ch; end; end; readln; end; procedure FindNextGap (line: LineType; length: integer; var gapPosition: integer); begin while line [gapPosition] = BLANK do begin gapPosition := gapPosition + 1; if gapPosition > length then begin gapPosition := 1; end; end; while line [gapPosition] <> BLANK do begin gapPosition := gapPosition + 1; if gapPosition > length then begin gapPosition := 1; end; end; end; procedure InsertOneBlank (var line: LineType; var length: integer; position: integer); var k: integer; begin for k := length + 1 downto position + 1 do begin line [k] := line [k-1]; end; line [position] := BLANK; length := length + 1; end; procedure InsertBlanks (var line: LineType; var actualLength: integer; desiredLength: integer); var gapPosition, k: integer; begin gapPosition := 1; for k := 1 to desiredLength-actualLength do begin FindNextGap (line, actualLength, gapPosition); InsertOneBlank (line, actualLength, gapPosition); end; end; procedure PrintLine (line: LineType; length: integer); var k: integer; begin if DEBUGGING then begin for k := 1 to length do begin write (k mod 10: 1); end; end; writeln; for k := 1 to length do begin write (line [k]); end; writeln; end; begin GetDesired (desiredLength); GetLine (line, actualLength); while actualLength > 0 do begin if line [1] = BLANK then begin writeln ('Line shouldn''t start with a blank.'); end else if line [actualLength] = BLANK then begin writeln ('Line shouldn''t end with a blank.'); end else if actualLength < desiredLength then begin InsertBlanks (line, actualLength, desiredLength); end; PrintLine (line, actualLength); GetLine (line, actualLength); end; end.