C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 6)
6.
What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input?
#include<stdio.h>
int main()
{
void fun();
fun();
printf("\n");
return 0;
}
void fun()
{
char c;
if((c = getchar())!= '\n')
fun();
printf("%c", c);
}
Answer: Option
Explanation:
Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc"
Output: cba
Discussion:
31 comments Page 1 of 4.
Manali said:
2 decades ago
Can you explain it to me. It took abc alltogether as an input, then printf will print it in reverse order? I am unable to understand it.
Sundar said:
1 decade ago
@All
In Turbo C, I have compiled the program myprogram.c and executed as given below.
C:\Turboc>myprogram.exe
kumar
ramuk
C:\Turboc>myprogram.exe
sundar kumar
ramuk radnus
C:\Turboc> _
Note: Give the input by simply typing any string and press.
The given program prints the given string in reversed order.
I hope this example will help you. Have a nice day!
In Turbo C, I have compiled the program myprogram.c and executed as given below.
C:\Turboc>myprogram.exe
kumar
ramuk
C:\Turboc>myprogram.exe
sundar kumar
ramuk radnus
C:\Turboc> _
Note: Give the input by simply typing any string and press
The given program prints the given string in reversed order.
I hope this example will help you. Have a nice day!
Sagar Kapadiya(Surat) said:
1 decade ago
Can you describe a whole program?
Reshma said:
1 decade ago
Can anybody tell me how abc is reversed to cba ?
Kamal dua said:
1 decade ago
It is based on flow of control.
Firstly in function fun() 'a' is entered then condition is checked. as it is not equal to '\0' so again fun() is called and this time 'b' is entered. similarly 'c' and at last in 4th recursive call of fuction fun()when enter is pressed, condition satisfies. so cursor goes to next line. now contol goes back to 3rd call of fun() in which c='c'.
Similarly control goes back to 2nd and 1st fun() call in which 'b' and 'a' get printed.
Firstly in function fun() 'a' is entered then condition is checked. as it is not equal to '\0' so again fun() is called and this time 'b' is entered. similarly 'c' and at last in 4th recursive call of fuction fun()when enter is pressed, condition satisfies. so cursor goes to next line. now contol goes back to 3rd call of fun() in which c='c'.
Similarly control goes back to 2nd and 1st fun() call in which 'b' and 'a' get printed.
Rohan said:
1 decade ago
Thanks kamal.
Navdeep Jain said:
1 decade ago
Its very simple if you have studied a little bit about STACKS.
Stack work in LIFO (Last In First Out) policy.
So, lets look at Push process -
First input, "a"
Second input, "b"
Last input, "c"
Now, Pop one by one and you get "cba".
Stack work in LIFO (Last In First Out) policy.
So, lets look at Push process -
First input, "a"
Second input, "b"
Last input, "c"
Now, Pop one by one and you get "cba".
Shubh said:
1 decade ago
I am still not understanding this program.
When does it asks to enter the character?
There is no printf statemnt asking the user to enter the character.
Someone please clear my this doubt.
@Kamal Dua:
As per your explanation, you said at 4th recursive call, the condition satisfies. How come at 4th? There is no. number 4 in the condition.
Hey can anyone clear it in more detailed way?
Thank You.
When does it asks to enter the character?
There is no printf statemnt asking the user to enter the character.
Someone please clear my this doubt.
@Kamal Dua:
As per your explanation, you said at 4th recursive call, the condition satisfies. How come at 4th? There is no. number 4 in the condition.
Hey can anyone clear it in more detailed way?
Thank You.
Manish said:
1 decade ago
@Shubh.
getchar() will take one character as input and matches it with \n and then make a recursive call if condition is satisfied.
getchar() will take one character as input and matches it with \n and then make a recursive call if condition is satisfied.
Anjan said:
1 decade ago
How %c can printing 3 value. I know it is childish question but please help me.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers