uses crt const am string type twords array of string var words twords

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
uses
crt;
const
s = ' I am a string ';
N = 4;
type
twords = array [1..N] of string;
var
words : twords;
ii : integer;
function get_words(s : string) : twords;
var
i, j, k, wstart : integer;
tail_found : boolean;
begin
i := 1; k := 0;
while i <= length(s) do
begin
if s[i] <> ' ' then
begin
wstart := i;
j := i + 1;
tail_found:=false;
while not tail_found do
begin
if (s[j] = ' ') or (j = length(s)+1) then
begin
tail_found := true;
inc(k);
i := j;
end;
inc(j);
end;
get_words[k] := copy(s, wstart, i - wstart);
end
else
inc(i);
end;
end;
procedure show_words(words : twords);
var
i : integer;
begin
for i:=1 to 4 do
writeln('"', words[i], '"');
end;
begin
clrscr;
writeln('"', s,'"'); writeln;
show_words(get_words(s));
end.