Answer to Question #274799 in C for n hasan

Question #274799

Write a program that reads in a sequence of characters and prints them in reverse order (Use a stack)?


1
Expert's answer
2021-12-02T18:04:15-0500
#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;
}

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