StreakPeaked· Practice

ExamsGATETechnical

Choose the correct option to fill ?1 and ?2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character. void reverse(void){ int c; if(?1) reverse(); ?2 } main(){ printf("Enter Text"); printf(" "); reverse(); printf(" "); }

  1. ?1 is (getchar() != ' ') ?2 is getchar(c);
  2. ?1 is (c = getchar()) != ' ' ?2 is getchar(c);
  3. ?1 is (c != ' ') ?2 is putchar(c);
  4. ?1 is ((c = getchar()) != ' ') ?2 is putchar(c);

Correct answer: ?1 is ((c = getchar()) != ' ') ?2 is putchar(c);

Solution

The correct option uses the assignment within the condition to read a character and check if it is not a newline. This allows the function to recursively call itself until the newline is encountered, and then it prints each character in reverse order as the recursion unwinds.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →