From b5fdf9af30b8275d8dbb48ef2c26abf78eef87cb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 30 Sep 2021 14:35:14 -0400 Subject: [PATCH] Add last_word example --- simple_examples/last_word.pas | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 simple_examples/last_word.pas diff --git a/simple_examples/last_word.pas b/simple_examples/last_word.pas new file mode 100644 index 0000000..e8840d3 --- /dev/null +++ b/simple_examples/last_word.pas @@ -0,0 +1,28 @@ +program last_word; + +{$mode objfpc}{$H+} + +type TCharSet = set of Char; + +function LastWord(const aPhrase: string; separators: TCharSet): String; +var L, p: integer; +begin + L := Length(aPhrase); + + if (L=0) or (separators=[]) then Exit(''); + + for p := L downto 1 do + if not (aPhrase[p] in separators) + then + Result := aPhrase[p] + Result + else Break; +end; + +var s: string; +begin + repeat + Write('Enter a phrase (or nothing to Quit): '); + ReadLn(s); + WriteLn('The last word of "', s, '" is: "', LastWord(s, [' ']), '"'); + until (s='') +end.