Stack to reverse string using python

Reverse String with help of stack

This Program is accepting string from user and with help of stack it will display the reverse of the string.

This program will help students to understand the concepts of stack and its working. This program is best for beginner as a starter.

Working of program.

  • Accept string from the user.
  • Scan complete string from left to right.
  • Push each element/character in stack.
  • Once scanning is done.
  • Start popping element from stack and append it to result list.
  • Once the stack is empty return the result in string form.
  • Display the result

There is only one end in stack from which element can be inserted/push or removed/pop. This feature helps in smooth execution of this program.

class  Stack_to_reverse  :
    # Creates  an  empty  stack.
    def	__init__(  self  ):
        self.items  =  list()
#Author : ITVoyagers Website :
    #Returns  True  if  the  stack  is  empty  or  False  otherwise.
    def  isEmpty(  self  ):
            return True
            return False
#Author : ITVoyagers Website :
    # Removes  and  returns  the  top  item  on  the  stack.
    def  pop(  self  ):
        if  self.isEmpty():
            print("Stack is empty")
            return self.items.pop()

    # Push  an  item  onto  the  top  of  the  stack.
    def  push(  self,  item  ):

    def reverse(self,string):
        n = len(string)
#Author : ITVoyagers Website :
 # Push all characters of string to stack
        for i in range(0,n):

 # Making the string empty since all characters are saved in stack

 # Pop all characters of string and put them back to string
#Author : ITVoyagers Website :
        for i in range(0,n):
        return string

seq=input("Enter a string to be reversedn")
sequence = S.reverse(seq)
print("Reversed string is " + sequence)
#Author : ITVoyagers Website :


Enter a string to be reversed
Reversed string is SREGAYOVTI

Note : Please note that above program is compatible with Python 3 or higher version.

You can also check following content on conversion, evaluation and other applications of stack

1 thought on “Stack to reverse string using python”

Leave a Comment