Write a program that reads in a sequence of characters and prints them in reverse order (Use a stack)?
#include <stdio.h>
#include <string.h>
#define MAX 100
int top=-1;
int val;
char S_string[MAX];
void Push_(char val);
char Pop_(void);
int isEmpty(void);
int isFull(void);
int main()
{
char S[MAX];
int x;
printf("Enter a string: ");
scanf("%[^\n]s",S);
for(x=0;x<strlen(S);x++)
Push_(S[x]);
for(x=0;x<strlen(S);x++)
S[x]=Pop_();
printf("Reversed String is: %s\n",S);
return 0;
}
void Push_(char val)
{
if(isFull())
{
printf("\nStack is FULL !!!\n");
return;
}
top=top+1;
S_string[top]=val;
}
char Pop_()
{
if(isEmpty())
{
printf("\nStack is EMPTY!!!\n");
return 0;
}
val = S_string[top];
top=top-1;
return val;
}
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}
Comments
Leave a comment