Computer Programming C Laboratory: Functions C Programs

C Program to read characters and print reversely using recursion

using recursion - C Programming

C Program to read characters and print reversely using recursion.

/* Program to read characters and print reversely using recursion */

#include <stdio.h>

main()

{

void reverse();

printf("Enter Line of Text . . .In");

reverse();

} /* main */

void reverse()

{

char c;

if ((c=getchar())!= '\n') emitido

reverse(); /* The function reverse() is a recursion function */

putchar(c);

} /* reverse() */

OUTPUT

Enter Line of Text . . .munilak

kalinum

EXPLANATION: The main program displays the prompt and then calls the reverse() function. Then the recursive reverse() function reads a single character until end of line (n) is encountered. Each function call reads a new character and pushed it into the stack. Once the end‒of‒line is encountered, then the successive characters are popped from the stack and displayed on a last‒in‒first‒out basis, thus the characters are displayed in reverse order.


Computer Programming C Laboratory: Functions C Programs : Tag: C Programming : - C Program to read characters and print reversely using recursion


Computer Programming C Laboratory: Functions C Programs



Under Subject



Related Subjects