Pascal 以递归方式反转字符串

Pascal 以递归方式反转字符串,pascal,Pascal,我想使用递归函数反转字符串,下面是我的代码: 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);

我想使用递归函数反转字符串,下面是我的代码:

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.

但是它不起作用,工作在哪里?

我不做Pascal,但是一个典型的(简单的)递归反转,在伪代码中,看起来像:

function reverse(word)
  if empty(word) return ""
  else return reverse(withoutFirstLetter(word)) + firstLetterOf(word)

我不使用Pascal,但在伪代码中,一个典型的(简单的)递归反转如下所示:

function reverse(word)
  if empty(word) return ""
  else return reverse(withoutFirstLetter(word)) + firstLetterOf(word)

是的,已编译,但输出太可怕了!!!那么输出是什么样子的呢?也许可以将它添加到您的问题中……例如,假设输入是“abc”,那么输出应该是“cba”,但它是“^$%\$^%$&%\$$%\\$%\$$%^$$%\\\$%\\$%\\$@$$%%\$%”或类似的东西……是的,已编译,但输出太可怕了!!!那么输出是什么样子的呢?例如,思考输入是“abc”,那么输出应该是“CBA”,但是它是“^ $%,$%$%&$$@ @ $$$^ ^ $$%,$@ %%@ $%@ %”或类似的东西…但是我需要Pascal一个,我可以为C++编写,但是在PASCAL中有一些不同…但是我需要Pascal一个,我真的可以为C++编写它,但是在帕斯卡语言中有一些不同..很好,接受了,非常感谢rene.很好,接受了,非常感谢rene.你的格式似乎不正确-用四个空格缩进行,将它们格式化为代码。另外,你能解释一下你的代码吗?如果你有pascal编译器,你可以试试。其思想是只使用一个参数递归地反转字符串。这个函数的复杂性仍然比另一个函数更大,因此它不是一个解决方案,但仅仅是对于Apparency来说,我们的格式似乎不正确-用四个空格缩进行以将它们格式化为代码。另外,你能解释一下你的代码吗?如果你有pascal编译器,你可以试试。其思想是只使用一个参数递归地反转字符串。这个函数的复杂性仍然大于另一个函数,因此它不是一个解决方案,只是针对apparenc
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;