0

再帰関数を使用して文字列を逆にしたいのですが、コードは次のとおりです。

Program InvertTheString;
var n:integer;word:string;

Function Invert (N:integer; word:string) : string;
begin
     if N=1 then
        Invert:=word[N]+Invert
     Else
        Invert:=Invert(N-1,word);
end;

BEGIN
readln(word);
n:=length(word);
writeln (Invert(N,word));

writeln;write('Press Enter To Exit...');
readln;
END.

しかし、それは機能していません、どこで着用されていますか?

4

3 に答える 3

3
Function Invert (N:integer; word:string) : string;
begin
     if N=0 then
        Invert:=''
     Else
        Invert:= word[N] + Invert(N-1,word);
end;
于 2012-12-25T17:02:59.243 に答える
2

私はPascalを実行しませんが、擬似コードでの典型的な(単純な)再帰的な逆は次のようになります。

function reverse(word)
  if empty(word) return ""
  else return reverse(withoutFirstLetter(word)) + firstLetterOf(word)
于 2012-12-25T16:54:40.297 に答える
0
Function Invert (ch:string) : string;
begin
 if ch='' then
 Invert:=''
 else
 {get the last letter of the string + eliminate it and execute the function}
 Invert:=copy(ch,length(ch),1)+Invert(copy(ch,1,length(ch)-1));
end;
于 2016-04-17T12:30:49.920 に答える