Naeel Maqsudov » 26 дек 2013, 17:47
В условии не сказано, что считать словом. Если словом считается любая подстрока, то можно так:
Код: Выделить всё
var
str: string;
TxtLen,L,P1,P2: integer;
function CheckPair(const str:string; pos1:integer; pos2:integer; len:integer):boolean;
var i:integer;
begin
for i:=1 to len do if str[pos1+i-1]<>str[pos2+len-i] then begin
CheckPair:=false;
exit;
end;
CheckPair:=true;
end;
begin
write('input text:'); readln(str);
TxtLen:=length(str);
for L:=2 to trunc(TxtLen/2) do begin
writeln('Searching for words with length ',L,'...');
for P1:=1 to L do begin
for P2:=P1+L to TxtLen-L+1 do begin
if CheckPair(str,P1,P2,L) then writeln('Pair was found. "',copy(str,P1,L),'"@',P1,' and "',copy(str,P2,L),'"@',P2);
end;
end;
end;
end.
В условии не сказано, что считать словом. Если словом считается любая подстрока, то можно так:
[code]
var
str: string;
TxtLen,L,P1,P2: integer;
function CheckPair(const str:string; pos1:integer; pos2:integer; len:integer):boolean;
var i:integer;
begin
for i:=1 to len do if str[pos1+i-1]<>str[pos2+len-i] then begin
CheckPair:=false;
exit;
end;
CheckPair:=true;
end;
begin
write('input text:'); readln(str);
TxtLen:=length(str);
for L:=2 to trunc(TxtLen/2) do begin
writeln('Searching for words with length ',L,'...');
for P1:=1 to L do begin
for P2:=P1+L to TxtLen-L+1 do begin
if CheckPair(str,P1,P2,L) then writeln('Pair was found. "',copy(str,P1,L),'"@',P1,' and "',copy(str,P2,L),'"@',P2);
end;
end;
end;
end.
[/code]