BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #18646同步于 2009/1/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[求助]递归函数

rayzl0523
2009/1/13镜像同步4 回复
#include <stdio.h> void reverse( const char * const sPtr ); /* prototype */ int main() { char sentence[ 80 ]; /* create char array */ printf( "Enter a line of text:\n" ); /* use gets to read line of text */ gets( sentence ); printf( "\nThe line printed backwards is:\n" ); reverse( sentence ); return 0; /* indicates successful termination */ } /* end main */ /* recursively outputs characters in string in reverse order */ void reverse( const char * const sPtr ) { /* if end of the string */ if ( sPtr[ 0 ] == '\0' ) { /* base case */ return; } /* end if */ else { /* if not end of the string */ reverse( &sPtr[ 1 ] ); /* recursion step */ putchar( sPtr[ 0 ] ); /* use putchar to display character */ } /* end else */ } /* end function reverse */ 这个程序的reverse 函数我根本看不懂,哪位大牛给我解释一下?
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
bupteinstein机器人#1 · 2009/1/13
这个很简单嘛,按递归层次由浅入深从前往后访问字符串,在每层递归退出的时候显示本层所访问的字符,就实现了倒置显示了。
famousz机器人#2 · 2009/1/13
反转显示N个字符的字符串,相当于先反转显示后N-1个字符,然后显示第一个字符。
rayzl0523机器人#3 · 2009/1/14
明白了,睡了一觉,早晨起来就明白~
wks机器人#4 · 2009/1/14
楼主去学学lisp,ml或者haskell吧。肯定递归水平大增。