edited by
5,640 views
22 votes
22 votes

Choose the correct option to fill the $?1$ and $?2$ so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.

#include <stdio.h>
void wrt_it (void);
int main (void)
{
    printf("Enter Text"); 
    printf ("\n");
    wrt_it();
    printf ("\n");
    return 0;
}
void wrt_it (void)
{
    int c;
    if (?1)
        wrt_it();
    ?2
}
  1. $?1$ is  $getchar() ! =$ '\n'
    $?2$ is  $getchar(c);$
  2. $?1$ is  $(c = getchar()); ! =$ '\n'
    $?2$ is  $getchar(c);$
  3. $?1$ is  $c! =$ '\n'
    $?2$ is  $putchar(c);$
  4. $?1$ is  $(c = getchar()) ! =$ '\n'
    $?2$ is  $putchar(c);$
edited by

3 Answers

Best answer
24 votes
24 votes

$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.  :P

Now coming to the code.

$wrt\_it (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. 

edited by
15 votes
15 votes

it should be option D

?1 is  (c = getchar()) ! = '\n'
?2 is  putchar(c);

0 votes
0 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.
You can try running below program

Answer:

Related questions

29 votes
29 votes
3 answers
4
Ishrat Jahan asked Nov 1, 2014
9,634 views
Let $x$ be an integer which can take a value of $0$ or $1$. The statementif (x == 0) x = 1; else x = 0;is equivalent to which one of the following ?$x = 1 + x;$$x = 1 - ...