Reverse String Using Stack : Data Structure
Tags : Reversal of String using Stack. String Reversal in C Programming | Learn Data Structure using C Programming.
Logic :
Accept Characters continuously and push characters onto stack.Accept characters till occurrence newline character is encountered.
while ((ch=getchar())!='n')
Accept characters “character by character” until entered character is “Newline Character“.For more information on getchar function Click Here
Program :
#include< stdio.h>#include< conio.h>#include< process.h># define MAX 50 typedef struct stack{ char data [MAX]; int top;}stack;//----------------------------int empty (stack *p){ if (p-> top == -1) return (1); else return (0);}//----------------------------int full (stack *p){ if (p->top == MAX-1) return (1); else return (0);}//----------------------------void push (stack *p, char x){ if(!full (p)) { p -> top = p -> top + 1; p -> data [p -> top] = x; } else printf ("n stack is full");}//-----------------------------------char pop (stack *p){ char x; if (!empty(p)) { x=p->data[p->top]; p->top = p->top - 1; return (x); } else { printf ("n stack is empty:"); return (''); }}//--------------------------------------void main (){ stack s; char ch; s.top = -1; printf ("nEnter a string:"); while ((ch=getchar())!='n') { if(!full(&s)) { push(&s, ch); } else { printf ("n stack is full…"); exit (0); } } printf ("nReversed String : "); while (!empty(&s)) { ch = pop(&s); printf("%c",ch); }}