What will be the output of the following program?main() { char str[]="san foundry"; int len = strlen(str); int i; for(i=0;i push(str[i]); // pushes an element into stack for(i=0;i pop(); //pops an element from the stack}
Question
What will be the output of the following program?main() { char str[]="san foundry"; int len = strlen(str); int i; for(i=0;i push(str[i]); // pushes an element into stack for(i=0;i pop(); //pops an element from the stack}
Solution
The program you've provided seems to be written in C, but it's incomplete and has some errors. It seems like it's trying to reverse a string using a stack. However, the functions push() and pop() are not defined, and the main() function doesn't have a return type.
Here's a corrected version of the program:
#include <stdio.h>
#include <string.h>
#define MAX 1000 // Maximum number of elements in the stack
char stack[MAX];
int top = -1;
void push(char c) {
if (top >= MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = c;
}
}
char pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int main() {
char str[] = "san foundry";
int len = strlen(str);
int i;
for (i = 0; i < len; i++) {
push(str[i]); // pushes an element into stack
}
for (i = 0; i < len; i++) {
printf("%c", pop()); // pops an element from the stack and prints it
}
return 0;
}
This program will push each character of the string "san foundry" onto a stack, then pop each character off the stack and print it. Since a stack is a LIFO (last in, first out) data structure, this will effectively print the string in reverse, resulting in the output "ydnuof nas".
Similar Questions
What will be the output of the following code?#include <stdio.h>int main() { char str[] = "Programming"; printf("%s\n", str + 3); return 0;}
What is the output of the following code snippet?void solve() { stack<int> s; s.push(1); s.push(2); s.push(3); for(int i = 1; i <= 3; i++) { cout << s.top() << “ “; s.pop(); }}
What is the output of the following code?char str[] = "hello";int len = strlen(str);printf("%d\n", len);
What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.