Reversing String Using Stack in C

Description

This Program is used to Reverse a Given String Using STACK in C


#include<stdio.h>
#define MAX 100
void input();
char stack[MAX];
int top= -1;
void push(char x)
{
 stack[++top]=x;
}

char pop()
{
 if(top==-1)
 return -1;
 else
 return stack[top--];
}

main()
{
 int choice;
 do
 { 
  printf("\n---MENU---\n1.INPUT\n2.EXIT\n");
  printf("\nEnter your Choice\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1 : input();break;
   case 2 : printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default : printf("\nINVALID INPUT !!!!\n");break;
  }
 }while(choice!=2);
}

void input()
{ 
 
 int i = 0; 
 char exp[100];
 printf("Enter a string :  ");
 scanf("%s",exp);
 
 while(exp[i]!='\0')
 {
  push(exp[i++]);
  
 }
 printf("\nReversed string : ");
 while(top!=-1)
 {
  printf("%c",pop());
 }
  
}


OUTPUT

Reversed String Output

Labels: