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,623 questions

55,358 answers

573 users

How to remove extra whitespace from a string in Pascal

1 Answer

0 votes
program NormalizeWhitespace;

{$mode objfpc}{$H+}

uses
  SysUtils; // CharInSet

{ ------------------------------------------------------------
  NormalizeWhitespace
  ------------------------------------------------------------
  Removes extra whitespace from a string:

  - Trim leading whitespace
  - Trim trailing whitespace
  - Collapse multiple internal whitespace into a single space

  The algorithm performs a single linear scan and writes into
  a result string. This avoids repeated allocations and keeps
  the logic simple and efficient.
  ------------------------------------------------------------ }
function NormalizeWhitespace(const S: string): string;
var
  i, j: Integer;
  InWS, Started: Boolean;
  Ch: Char;
begin
  SetLength(Result, Length(S));  { Preallocate for efficiency }
  j := 0;
  InWS := False;
  Started := False;

  for i := 1 to Length(S) do
  begin
    Ch := S[i];

    if CharInSet(Ch, [' ', #9, #10, #13]) then
    begin
      { Skip leading whitespace }
      if not Started then
        Continue;

      { Skip repeated whitespace }
      if InWS then
        Continue;

      { First whitespace after a word → write a single space }
      Inc(j);
      Result[j] := ' ';
      InWS := True;
    end
    else
    begin
      { Non-whitespace character }
      Inc(j);
      Result[j] := Ch;
      InWS := False;
      Started := True;
    end;
  end;

  { Remove trailing space if present }
  if (j > 0) and (Result[j] = ' ') then
    Dec(j);

  SetLength(Result, j);
end;

{ ------------------------------------------------------------
  Main program
  ------------------------------------------------------------ }
var
  S, Cleaned: string;
begin
  S := '   This   is   a   test   string   with         extra   spaces.   ';

  Cleaned := NormalizeWhitespace(S);

  WriteLn('Original: [', S, ']');
  WriteLn('Cleaned:  [', Cleaned, ']');
end.



(*
run:

Original: [   This   is   a   test   string   with         extra   spaces.   ]
Cleaned:  [This is a test string with extra spaces.]

*)

 



answered 1 day ago by avibootz
...