GATE Computer ScienceHardGATE CS (PYQ pattern)

void r(char *s) { if (s[0] == 0 || s[1] == 0) { printf("%s", s); return; } printf("%c%c", s[1], s[0]); r(s + 2); printf("%c", s[0]); } What does r("abcdefg") print?

  1. A. badcfegace
  2. B. badcfegeca
  3. C. badcfeg
  4. D. badcfegcea

Correct answer: B. badcfegeca

Going down the pairs are printed swapped: ba, dc, fe; the base case prints g; coming back each level prints its first character, from the deepest: e, c, a.

More Programming and Data Structures (GATE) questions

Exams that ask this topic