Recent

Author Topic: reading strings  (Read 2793 times)

xdv

  • Newbie
  • Posts: 1
reading strings
« on: January 08, 2025, 07:46:46 pm »
Code: Pascal  [Select][+][-]
  1. //fpc 3.0.4
  2.  
  3. program HelloWorld;
  4. var i:integer;
  5.     s:string;
  6. begin
  7.  
  8.     for i:=1 to 4 do begin
  9.                 read(s);writeln(s);
  10.                 end;
  11.      writeln(s);
  12. end.
  13.  

how to read seperate strings?

Code: Text  [Select][+][-]
  1. opel toyota bmw mercedes

VisualLab

  • Hero Member
  • *****
  • Posts: 693
Re: reading strings
« Reply #1 on: January 08, 2025, 08:26:40 pm »
For example like this:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. const
  7.   STitleContent = 'Read strings from CLI';
  8.   STitleUnderline = '----------------------';
  9.   SQuestion = 'Enter the names of the cars: ';
  10.   SSeparator = ' ';
  11.   SAnswerInfo: array [1..2] of String = ('You provided', 'car names: ');
  12.   SAnswerPoint: array [1..2] of String = (' [', '] ');
  13.   SAnswerNameSeparator = ', ';
  14.  
  15. var
  16.   i: Integer;
  17.   GCarNameStr: String;
  18.   GStrNameArray: array of String;
  19.  
  20. begin
  21.   WriteLn(STitleContent);
  22.   WriteLn(STitleUnderline);
  23.   WriteLn;
  24.   Write(SQuestion);
  25.   ReadLn(GCarNameStr);
  26.   GStrNameArray := GCarNameStr.Split([SSeparator]);
  27.   WriteLn;
  28.   WriteLn(SAnswerInfo[1], SSeparator, Length(GStrNameArray), SSeparator, SAnswerInfo[2]);
  29.   for i := Low(GStrNameArray) to High(GStrNameArray) do
  30.    begin
  31.     Write(SAnswerPoint[1], i, SAnswerPoint[2], GStrNameArray[i]);
  32.     if i < High(GStrNameArray)
  33.      then Write(SAnswerNameSeparator);
  34.     WriteLn;
  35.    end;
  36.   ReadLn;
  37. end.

Compiled in Lazarus 3.4 on Windows 10 64-bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1623
Re: reading strings
« Reply #2 on: January 08, 2025, 09:05:57 pm »
Similar to VisualLab's, but simpler:
Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. var s, s1:string;
  4.  
  5. begin
  6.   readln(s);
  7.   For s1 in s.Split(' ') do
  8.     WriteLn(s1);
  9. end.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 689
Re: reading strings
« Reply #3 on: January 08, 2025, 10:48:36 pm »
Similar to VisualLab's, but simpler:
Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. var s, s1:string;
  4.  
  5. begin
  6.   readln(s);
  7.   For s1 in s.Split(' ') do
  8.     WriteLn(s1);
  9. end.

Not your father's Pascal -- but nice.

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: reading strings
« Reply #4 on: January 09, 2025, 02:31:31 am »
There is a marker that says 3.0.4.
I am not sure the type helper for split was already in that release, which is very,very old.The code is OK and recommended, but maybe @xdv needs to upgrade first to 3.2.x?
I know the helper syntax was introduced in 3.0.0 but I am not sure about the sysutils implementation of all the standard helpers.
« Last Edit: January 09, 2025, 02:37:43 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018