Page 167 - DCAP407_DATA_STRUCTURE
P. 167
Data Structure
main ()
{
/* A character array S whose size is 80 is declared */
char S[80];
printf ("\n Enter a string: \n");
/* gets function declared in stdio.h header file reads a line from standard
input and stores it in S. */
gets (S);
printf("\nThe reversed string: \n");
/* print_reverse function is called with the argument S */
print_reverse (S);
/* Waits until a key is pressed */
getch();
}
Output:
Enter the string to be reversed: english
Output String : hsilgne
In this example:
1. First, the stdio.h header file is included using the include keyword.
2. Then, a recursive function named print_reverse is declared. The function
takes a parameter S, which is a character pointer.
3. In the print_reverse () method:
(a) First, using an ‘if’ condition, the value of string pointer S is checked. If
the value is not equal to NULL, a recursive call is made by passing the
incremented value of S.
(b) Finally, the value of S is decremented and the string pointer value is
written using putchar().
4. Then, the main () function is executed.
5. Then, a character array S whose size is 80, is declared.
6. Then, gets function is used to read a line from the standard input and this is
stored in S.
7. Then, print_reverse function is invoked with the argument S.
8. Finally, getch() prompts the user to press any key and when the key is
pressed, the program is terminated.
160 LOVELY PROFESSIONAL UNIVERSITY