The Gateway to Computer Science Excellence
+16 votes
2.4k views

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);$

in Programming by
edited by | 2.4k views
0
"Since assignment is an operator in C just like +,the act of assignment produces a result,just as adding two numbers produces a result."

"The result of an assignment $v=e$ is the value of v after the assignment"

6 Answers

+26 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.

by
edited by
0
how is this reversed??
0
please elaborate..
0
explain please
+2
The question has few lines missing. Proper explanation is given here:

http://www.geeksforgeeks.org/c-language-set-5/
0
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?
0
@Srestha, by backtracking function call stack, printing will be done. No need of incrementing.
0

@ Swati Rauniyar  

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

+3
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.
+7 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)
by
+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.
by
+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. 

by
0 votes
void reverse(void)
{
int c;
if(?1) reverse( );
?2
}
main( )
{
printf(“Enter Text”);
printf(“\n”);
reverse( );
printf(“\n”);
}
We can simply eliminate A & B for ?2.
& Hence
?1 is ((c=getchar( )) != ‘\n’)
?2 is putchar(c);
by
0 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.
by
Answer:

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
52,345 questions
60,468 answers
201,792 comments
95,271 users