Skip to main content

Posts

Showing posts from February 9, 2013

Reverse a string using stack

LIST OF PROGRAMS #include<stdio.h> #include<conio.h> #include<process.h> void push(); void display(); int top=-1,ch=1,lim,tmp=-1; char stack[100],item; void push() { if(top<lim-1) { printf("ENTER CHARACTER TO BE STORED"); fflush(stdin); scanf("%c",&item); top=top+1; stack[top]=item; } else printf("STACK OVERFLOW"); } void display() { tmp=top; tmp=0; printf("\nORIGINAL STRING "); while(tmp<=top) printf("%c",stack[tmp++]); printf("\nREVERSED STRING "); while(tmp>=0) { printf("%c",stack[tmp--]); } } void main() { clrscr(); printf("ENTER MAXIMUM LIMIT "); scanf("%d",&lim); do { printf("\n ENTER 1 FOR PUSH\nENTER 2 FOR DISPLAY \n ENTER ANY OTHER NUMBER FOR EXIT "); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1 : push(); break; case 2: display(); break; default:exit(0); } }while(ch==1