The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+13 votes

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 new line character.

void reverse(void) 
    int c;
    if(?1) reverse();
    printf("Enter text"); 
  1. $?1$ is $(getchar() != ’\setminus n’)$

    $?2$ is $getchar(c);$
  2. $?1$ is $((c = getchar() ) !=’\setminus n’)$

    $?2$ is $getchar(c);$

  3. $?1$ is $(c != ’\setminus n’)$

    $?2$ is $putchar(c);$

  4. $?1$ is $((c = getchar() ) != ’\setminus n’)$

    $?2$ is $putchar(c);$

asked in Programming by Veteran (52.1k points)
edited by | 1.6k views

4 Answers

+22 votes
Best answer

Here, we are using the '$=$' operator which has less priority than '$!=$' operator. So $(c=getchar())$ has to be in brackets and after reversing the string we use function $putchar(c)$ for printing the character.

So, option (D) is the right answer.

answered by Active (3.3k points)
edited by
how is this reversed??
please elaborate..
explain please
The question has few lines missing. Proper explanation is given here:
But for reversing there need to be a pointer increment statement.Otherwise it will go to infinite loop. So, how will the string reverse without this increment statement?
@Srestha, by backtracking function call stack, printing will be done. No need of incrementing.

@ Swati Rauniyar  

u mean after printing stack will remove that character from the stack, so there will be no need of pointer.

Yes. Function call from A to B to C to D. Space came, return back on stack. Now backtrack. Print D to Print C to Print B to Print A.
+6 votes
getchar() is used to get the input character from the user and putchar() to print the entered character, but before printing reverse is called again and again until ‘\n’ is entered. When ‘\n’ is entered the functions from the function stack run putchar() statements one by one. Therefore, last entered character is printed first.


answered by (393 points)
+3 votes
This problem can be easily solved by elimination.
The output should print the string in reverse order. So option (A) and (B) can be avoided since it do not contain any function call to print. For option (C) and (D), Only (D) has the function to get input. So (D) is the answer.
This reverses the string using recursive approach.
answered by (441 points)
+1 vote

$getchar()$ - reads a single character at a time from the $stdin$.

$putchar(c)$ - writes a character specified by the argument to $stdout$.

As getchar() and putchar() both are needed to read the string and print its reverse and only option D contains both the function. D is the answer. 

Now coming to the code.

$reverse(void)$ is calling itself recursively. when \n is encountered $putchar()$ gets executed and prints the last character and then the function returns to its previous call and prints last 2nd character and so on. 

answered by Active (4.5k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,807 questions
54,504 answers
74,885 users