The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+13 votes
731 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 | 731 views
+2
/n in options should be replaced with \n
0
yes. changed :)

3 Answers

+10 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 (13.3k 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" ?
+9

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

+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 (501 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

40,976 questions
47,609 answers
146,779 comments
62,342 users