What will be the output of the following program?
Asked In:
BPSC TRE 3.0
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "san foundry";
int len = strlen(str);
int i;
for(i = 0; i < len; i++)
push(str[i]); // pushes each character into stack
for(i = 0; i < len; i++)
printf("%c", pop()); // pops and prints from stack
}
Options
-
A.
yrdnuof nas
-
B.
foundry nas
-
C.
sanfoundry
-
D.
san foundry
Correct Answer (Detailed Explanation is Below)
A.
yrdnuof nas
Detailed Explanation
Step 1: Push all characters into stack
Stack (top at right):
s a n f o u n d r y
Step 2: Pop all characters from stack
Stack follows LIFO (Last In First Out)
So popping will reverse the string.
Reversed string:
yrdnuof nas