Answer to Question #240108 in C for Smiley

Question #240108

Write a C code to implement the following operations using stack. 

Assume the Stack S is empty initially.

Read the given text character by character.

If the character read is an operand push it onto the stack.

If the character read is a special character do the following

* means pop 

$ means two pops

# means pop and a duplicate of popped value

Example: 

DAT*ASTRU$$CTURES**#ANDAL$$GOR##ITHM**S

Stack:

S

T

I

G

A

U

T

C

A

A

D

Output: TURTSSERRLADNRROOMH


1
Expert's answer
2021-09-22T00:05:49-0400
#include<stdio.h>
int top=-1;
char arr[15];
void pop();
void push(char x);
void display();
 
void push(char x)
{


top++;
arr[top]=x;


}
void pop(){
	int x = top;
	top--;
	printf("Element removed from stack is: %c\n",arr[x]);
	
}
void display(){
	int i;
	for(i=0; i<top; i++){
		printf("%c   ",arr[i]);
	}
}
int main(){
	char string[50] = "DAT*ASTRU$$CTURES**#ANDAL$$GOR##ITHM**S";
	int i;
	for(i=0; i<50; i++){
		char c = string[i];
		if(c=='*' || c=='$' || c=='#'){
		
		}
		else{
			push(c);
		}
	}
	printf("Stack size:%d\n", top);
	display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS