GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Reverse input by printing on recursive return

3 questions solved the same way, newest first. Back to Programming in C

How these are solved

Read or index one character, stop at the stated sentinel, recursively process the remainder, then emit the saved character on unwind. The 2008 task fills blanks and prints a newline at its base;

1 mark · MCQ
Report issue

Print on recursive return to reverse input

Recursion

Question 19, GATE 2024: main calls fX once. The function has local char a; it reads a=getchar(), recursively calls fX if a is not newline, then prints a with putchar if a is not newline. Input is 1234 followed by newline. A no termination; B termination with no output; C termination with 4321; D termination with 1234.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Approach: Distinguish the order in which recursive calls read characters from the order in which suspended calls resume printing.
1. The first call stores '1' in its own local a and calls fX again before reaching putchar. The next calls similarly store '2', '3' and '4'. Each call has a separate local variable.
2. A fifth call reads the newline. Both conditions are false, so this call neither recurses nor prints and returns immediately.
3. The suspended call holding '4' now continues at its second condition and prints 4. Returning again prints 3, then 2, then 1.
4. Once the first call returns, main terminates. The newline itself is never printed.
Answer: C, output 4321. Printing before the recursive call would preserve input order; printing after it reverses order. The explicitly supplied newline is the stopping condition, so no assumption about reading past end-of-file is needed.
The printed program uses typographic single quotation marks. Taken literally these are not valid C delimiters, so the program does not compile as printed. The answer above follows the intended textbook interpretation with ASCII quote delimiters.

Original paper, page 13 · Answer key

1 mark · MCQ
Report issue

Recursion

Question 21, GATE 2015: C function foo(char*a){if(*a && *a!=' '){foo(a+1); putchar(*a); }}. Output for string “ABCD EFGH”? A ABCD EFGH; B ABCD; C HGFE DCBA; D DCBA.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Recursion stops at the first space without advancing beyond it. Each earlier character is printed only after its recursive call returns, reversing ABCD and producing DCBA, D.

The condition stops recursion when the pointer reaches either the terminator or the first space. Starting at A, the function calls on B, C, D and then the space, where it returns without printing. Printing occurs after each recursive call, so unwinding emits D, C, B, A. No invocation reaches EFGH. Had putchar preceded the recursive call, the first word would print forward instead.

Original paper, page 33 · Answer key

2 marks · MCQ
Report issue

Recursive string reversal

Recursion

Question 61, GATE 2008: Fill ?1 and ?2 so this historical C program reverses a newline-terminated input string.
void reverse(void) {
 int c;
 if (?1) reverse();
 ?2
}
main() {
 printf("Enter Text"); printf("\n");
 reverse(); printf("\n");
}

(A) ?1: (getchar()!='\n'); ?2: getchar(c);
(B) ?1: ((c=getchar())!='\n'); ?2: getchar(c);
(C) ?1: (c!='\n'); ?2: putchar(c);
(D) ?1: ((c=getchar())!='\n'); ?2: putchar(c);
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Each invocation must read and retain its own character: ((c=getchar()) != '\n'). Recurse only for a non-newline character, then putchar(c) while unwinding.
2. This prints the input characters in reverse, option D. As written, the base invocation also prints the terminating newline before the reversed characters; main then prints a newline afterward.
3. A and B use getchar instead of output; C tests an uninitialized c. The source uses legacy implicit-int main; interpret the algorithm under its historical C conventions.

Original paper, page 12