The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+14 votes
890 views

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);$
asked in Programming by Boss (19.1k points)
edited by | 890 views
+2
/n in options should be replaced with \n
0
yes. changed :)
0
C is in int and getchar is character!

3 Answers

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

answered by Boss (15.4k points)
edited by
0
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" ?
+10

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 ....!!

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

@Soumya29

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

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. 

+1
ya my bad :(

ASCII of \0 == 0 in C

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

Thanks :)
0

@ 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
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.
+15 votes

it should be option D

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

answered by Active (2.1k points)
0

Why the code showing error?

                        #include <stdio.h>
                        void wrt_it (void);
                        int main (void)
                        {
                        	char a[5]="ABCDE";
                            printf("%s",a); 
                            printf ("\n");
                            wrt_it();
                            printf ("\n");
                            return 0;
                        }
                        void wrt_it (void)
                        {
                            int c;
                            if ((c = getchar())!='\n')
                                wrt_it();
                            putchar(c);
                        }

+1

@shreya .. I found no error check here : http://ideone.com/1nHHtI 

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

answered by Junior (669 points)
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
47,928 questions
52,333 answers
182,380 comments
67,805 users