in Programming in C edited by
5,675 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);$
in Programming in C edited by
5.7k views

4 Comments

edited by
0
0
Whats the problem in that? The question asks about the entered string only – not the newline.
0
0
Sir, it is printing newline also and no one is talking about it that’s why i asked but now i am realizing it is not much relevant. it just shifts the output in new line :)
0
0

3 Answers

24 votes
24 votes
Best answer

$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

10 Comments

If the string is "abcd" which obviously ends with a "\0", then won't the top most element of recursion stack print "\0", followed by   "d" from the next top element of recursion stack.

So finally shoud not it print "\0dcba" ?
0
0

No.. getchar(); reads single character at a time from the stdin. 

void wrt_it (void)
{
  int c;
  if ((c = getchar())!='\n')
    wrt_it();
  putchar(c);
}

wrt_it(); keep calling itself until \n is encountered.

a         c=getchar()) => c=a  => a !='\n' . condition satisfied . Entered "if" block.

b         c= getchar()) => c=b  => b !='\n' . condition satisfied . Entered "if" block.

c         c= getchar()) => c=c  => c !='\n' . condition satisfied . Entered "if" block.

d         c= getchar()) => c=d  => d !='\n' . condition satisfied . Entered  "if" block.

now you hit ENTER.   c= getchar()) => c=\n  => \n !='\n' . false . 

print dcba ....!!

17
17
Can someone explain what is the relevance of int c? why is it taken of type int?
1
1

@Soumya29

In geneal we take NULL comparison as false  NULL == NULL as false ==> NULL != NULL become TRUE right ??

0
0

@jatin  khachane, it happens in SQL right?
Here newline characters (ASCII code = 10) will be compared.
Even when '\0' s are compared, it will not return false as 0==0 will be evaluated as true. 

0
0
ya my bad :(

ASCII of \0 == 0 in C

and moreover its \n here not \0 here right ? :(

Thanks :)
1
1

@ I can't understand the line in your comment on Oct 9,2017

now you hit ENTER.   c= getchar()) => c=\n  => \n !='\n' . false . 

print dcba ....!!

If the condition is getting false when c= /n  the it will go to else part which is putchar(c), i.e putchar(\n). So \n will be printed first.

please clear my doubt.

0
0
Yes, '\n' this will be printed, Which is new line So, before the actual output DCBA(if input is ABCD) there will be a newline above this.
3
3
coz getchar() returns the ASCII value of characters taken from STDIN . Same is applicable for putchar() as well , it too takes the ASCII value of characters and prints the actual character into STDOUT
0
0

the output would be \nELPPA  if input is APPLLE\n

so option D 

0
0
15 votes
15 votes

it should be option D

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

4 Comments

implementation

1
1

@Kabir5454 @Arjun  sir

Why char and int both are working fine.

i.e. c is of type int and taking char as well as integer.

0
0

@GateOverflow04 Because every character is appropriately converted to int and then back to char.

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