Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to wrap a string into lines of width w in Pascal

2 Answers

0 votes
program WrapTextProgram;

{$mode objfpc}{$H+}{$J-}

{
    This program wraps a string into lines of maximum width `w`.

    Method:
    - Use TStringList to split the input text into words.
    - Build each line until adding another word would exceed the width.
    - When the limit is reached, store the line and begin a new one.
    - Uses standard string operations (Length, concatenation) for clarity.
}

uses
    SysUtils, Classes;

function WrapText(const S: string; W: Integer): string;
var
    WordList: TStringList;
    Line, ResultText, Temp: string;
    I: Integer;
begin
    { Create a TStringList to hold words }
    WordList := TStringList.Create;
    try
        WordList.Delimiter := ' ';
        WordList.StrictDelimiter := True;
        WordList.DelimitedText := S;

        Line := '';
        ResultText := '';

        { Build wrapped lines from words in WordList }
        for I := 0 to WordList.Count - 1 do
        begin
            Temp := WordList[I];

            { If line is empty, start it with the word }
            if Line = '' then
                Line := Temp
            else
            begin
                { Check if adding the next word exceeds width }
                if Length(Line) + 1 + Length(Temp) <= W then
                    Line := Line + ' ' + Temp
                else
                begin
                    { Store the completed line }
                    ResultText := ResultText + Line + LineEnding;
                    Line := Temp;
                end;
            end;
        end;

        { Add the final line }
        if Line <> '' then
            ResultText := ResultText + Line;

        Result := ResultText;
    finally
        WordList.Free;
    end;
end;

var
    Sample, Wrapped: string;

begin
    Sample :=
        'Free Pascal provides useful built-in functions for handling strings. ' +
        'This program demonstrates how to wrap text cleanly and efficiently.';

    Wrapped := WrapText(Sample, 35);
    WriteLn(Wrapped);
end.


{
run:

Free Pascal provides useful
built-in functions for handling
strings. This program demonstrates
how to wrap text cleanly and
efficiently.

}

 



answered Jul 11 by avibootz
edited Jul 11 by avibootz
0 votes
program WrapTextProgram;

{$mode objfpc}{$H+}{$J-}

uses
  SysUtils;

var
  s, wrapped: string;

begin
    s := 'Free Pascal provides useful built-in functions for handling strings. ' +
         'This program demonstrates how to wrap text cleanly and efficiently.';

    Wrapped := WrapText(s, 35);
    WriteLn(Wrapped);
end.



{
run:

Free Pascal provides useful built-in 
functions for handling strings. This 
program demonstrates how to wrap text 
cleanly and efficiently.

}

 



answered Jul 11 by avibootz
...