Mn giúp mình với mình ms học lập trình mà mình được biết khi gặp return sẽ thoát chương trình mà sao hàm đệ quy này lại cho kết quả như vậy.
#include <stdio.h>
#define SIZE 80
void reverse( const char * const sPtr );
int main( void )
{
char sentence[ SIZE ]; // create char array
puts( "Enter a line of text:" );
fgets( sentence, SIZE, stdin );
puts( "\nThe line printed backward is:" );
reverse(sentence);
} // end main
// recursively outputs characters in string in reverse order
void reverse( const char * const sPtr )
{
// if end of the string
if ( '\0' == sPtr[ 0 ] ) {
// base case
return;
} // end if
else {
reverse( &sPtr[ 1 ] ); // recursion step
putchar( sPtr[ 0 ] );// if not end of the strin
} // end else
} // end function reverse
Kết quả:
Enter a line of text:
Characters and Strings
The line printed backward is:
sgnirtS dna sretcarahC

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?