edited by
8,065 views
28 votes
28 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();
    ?2 
} 
main() 
{ 
    printf("Enter text"); 
    printf("\n"); 
    reverse(); 
    printf("\n"); 
}
  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);$

edited by

5 Answers

1 votes
1 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. 

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. 

Answer:

Related questions

26 votes
26 votes
3 answers
2
Kathleen asked Sep 11, 2014
12,587 views
Which combination of the integer variables $x, y,$ and $z$ makes the variable $a$ get the value $4$ in the following expression?$$a=(x y)?((x z) ?x:z): ((y z) ?y:z)$$$...