Sunday, 7 April 2013

String reverse using stack in c program with example

REVERSE A STRING USING STACK


#include
#include
typedef struct stack
{
char b[100];
int top;
}stack;
void push(stack *s,char k)
{
if(s->top==99)
printf("\n Stack is full ");
else
s->b[++s->top]=k;
}
char pop(stack *s)
{
char c;
if(s->top==(-1))
printf("\n Stack is empty");
else
else
c=s->b[s->top--];
return c;
}
void main()
{
char name[100];
stack s;
clrscr();
s.top=-1;
printf("\nEnter name :");
gets(name);
int i=0;
while(name[i]!='\0')
{
push(&s,name[i]);
i++;
}
printf("\n Reverse string is :");
while(s.top!=-1)
{
printf("%c",pop(&s));
}
getch();
}
 
Output:
 
Enter name :failmark
 
Reverse string is :kramliaf

No comments:

Post a Comment