edited by
7,812 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

Best answer
37 votes
37 votes

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.

edited by
9 votes
9 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.

 

Answer(D)
5 votes
5 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.
3 votes
3 votes
#include<stdio.h>

void reverse(void) {

 int c;

 if ((c=getchar())!='\n') reverse();

 putchar(c);

}

main(){

 printf ("Enter Text ") ;

 printf ("\n") ;

 reverse();

 printf ("\n") ;

}

Here c is declared as int.

Unlike some other languages you may have used, chars in C are integers. char is just another integer type, usually 8 bits and smaller than int, but still an integer type.

In C we can convert between char and other integer types using a cast, or just by assigning.

Unless EOF occurs, getchar() is defined to return "an unsigned char converted to an int"  so if it helps you can imagine that it reads some char, c, then returns (int)(unsigned char)c.

[note:we can convert this back to an unsigned char just by a cast or assignment, we can convert it to a char with a cast or by assigning it to a char.]

 

Recursion:

Eg string = load

we get the input text character by character using getchar() function until it get new line character. after taking each character the function call itself until is reach the end-of-line and the get the newline character. When it reaches the end-of-line then it start to print using putchar() function, (so first '\n' will be printed) and return to the previous recursive call of the function in the recursion and print the second last character (here 'd') and return to the upper function which call this function(which will putchar - a), this will recursively happens till the initial call of reverse.
Answer:

Related questions

26 votes
26 votes
3 answers
2
Kathleen asked Sep 11, 2014
12,439 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)$$$...