• edited by
11,930 views
35 35 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);$

Related Questions :

3 Answers

Best answer
36 36 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 15 votes

it should be option D

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

3 3 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:
Position:
Show:

Related questions

51 51 votes
3 answers 3 answers
16.1k
16.1k views
Ishrat Jahan asked Nov 2, 2014
16,085 views
Consider the following C program:#include <stdio.h typedef struct { char *a; char *b; } t; void f1 (t s); void f2 (t *p); main() { static t s = {"A", "B"}; printf ("%s %s...
60 60 votes
3 answers 3 answers
15.9k
15.9k views
Ishrat Jahan asked Nov 2, 2014
15,908 views
What is the output of the following program?#include<stdio.h int funcf (int x); int funcg (int y); main () { int x = 5, y = 10, count; for (count = 1; count <= 2; ++count...
57 57 votes
6 answers 6 answers
19.4k
19.4k views
Ishrat Jahan asked Nov 2, 2014
19,423 views
Consider the following C program which is supposed to compute the transpose of a given $4 \times 4$ matrix $M$. Note that, there is an $X$ in the program which indicates ...
51 51 votes
5 answers 5 answers
17.8k
17.8k views
Ishrat Jahan asked Nov 1, 2014
17,752 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 - ...