Add last_word example
This commit is contained in:
parent
1a524dc853
commit
b5fdf9af30
28
simple_examples/last_word.pas
Normal file
28
simple_examples/last_word.pas
Normal file
@ -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.
|
Loading…
Reference in New Issue
Block a user