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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to remove newlines from a string Pascal

2 Answers

0 votes
program RemoveNewLinesProgram;
 
function RemoveNewLines(const s: string): string;
var
  i: Integer;
begin
  RemoveNewLines := '';
  for i := 1 to Length(s) do
    if not (s[i] in [#10, #13]) then
      RemoveNewLines := RemoveNewLines + s[i];
end;
 
var
  s: string;
begin
  s := 'pascal' + #10 + ' c c++ ' + #13 + ' java python' + #13 + 'go';
  s := RemoveNewLines(s);
  WriteLn(s);
end.
 
 
 
(*
run:
 
pascal c c++  java pythongo
 
*)

 



answered 5 hours ago by avibootz
edited 5 hours ago by avibootz
0 votes
program RemoveNewLinesProgram;
 
function RemoveNewLines(const s: string): string;
var
  i: Integer;
  lastWasSpace: Boolean;
begin
  RemoveNewLines := '';
  lastWasSpace := False;
 
  for i := 1 to Length(s) do
  begin
    case s[i] of
      #10, #13:
        begin
          { Treat newline as a space }
          if not lastWasSpace then
          begin
            RemoveNewLines := RemoveNewLines + ' ';
            lastWasSpace := True;
          end;
        end;
 
      ' ':
        begin
          { Collapse multiple spaces }
          if not lastWasSpace then
          begin
            RemoveNewLines := RemoveNewLines + ' ';
            lastWasSpace := True;
          end;
        end;
 
    else
      RemoveNewLines := RemoveNewLines + s[i];
      lastWasSpace := False;
    end;
  end;
 
  { Trim trailing space if any }
  if (Length(RemoveNewLines) > 0) and (RemoveNewLines[Length(RemoveNewLines)] = ' ') then
    Delete(RemoveNewLines, Length(RemoveNewLines), 1);
end;
 
var
  s: string;
begin
  s := 'pascal' + #10 + ' c c++ ' + #13 + ' java python' + #13 + 'go';
  s := RemoveNewLines(s); // result without extra spaces
  WriteLn(s);
end.
 
 
 
 
(*
run:
 
pascal c c++ java python go
 
*)

 



answered 5 hours ago by avibootz
edited 4 hours ago by avibootz
...