Cách 1:
program Hello;
uses crt;
var s,tg,kq:string;
i:integer;
begin
clrscr;
write('nhap s: ');
readln(s);
for i:=length(s) downto 1 do (chạy theo chiều nghịch)
begin
if s[i] <> ' ' then
tg:=s[i]+tg;
if (s[i]=' ') or (i=1) then
begin
kq:=kq+tg+' ';
tg:='';
end;
end;
Writeln(kq);
end.
Cách 2:
program Hello;
uses crt;
var s,tg,kq:string;
i:integer;
begin
clrscr;
write('nhap s: ');
readln(s);
for i:=1 to length(s) do (chạy theo chiều thuận)
begin
if s[i] <> ' ' then
tg:=tg+s[i];
if (s[i]=' ') or (i=length(s)) then
begin
kq:=' '+tg+kq;
tg:='';
end;
end;
Writeln(kq);
end.
s[i]: truy cập vào từng ký tự trong chuỗi theo chỉ số (như mảng).