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 回答