[Récursivité] Fonction inversant une chaîne
Bonjour,
Je suis en train d'apprendre les algorithmes récursifs au lycée. On me propose d'écrire une fonction récursive qui inverse une chaine donnée.
Code:
1 2 3 4 5
| function inv(ch:string):string;
begin
if ch='' then inv:=''
else inv:=ch[length(ch)] + inv(copy(ch,1,length(ch)-1));
end; |
Ceci est proposé par le professeur mais ne fonctionne pas.
Moi, je propose plutôt ça qui n'arrange pas :
Code:
1 2 3 4
| function inv(ch:string):string;
begin
if ch<>'' then inv:=ch[length(ch)] + inv(copy(ch,1,length(ch)-1));
end; |
Et voici tout le programme qui ne fonctionne pas non plus :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| program rec;
uses wincrt;
var ch:string;
function inv(ch:string):string;
begin
if ch='' then inv:=''
else inv:=ch[length(ch)] + inv(copy(ch,1,length(ch)-1));
end;
BEGIN
ch:='SRUEPPOLEVEDSEDMUROF';
writeln(inv(ch));
END. |
ça affiche Run Time Error...
Merci de m'avoir lu!