Python Program to Reverse String Using Stack

Reverse a string using stack in python; In this python post, You will learn how to reverse a string using stack in python.

While you implement this program, you need to know the inserted/push or removed/pop element in the stack. This feature helps in the smooth execution of this program.

Stack To Reverse String Using Python

  • Python Program to Reverse a String using Stack

Python Program to Reverse a String using Stack

Follow below-given steps for write a program to reverse a string using stack in Python:

  • Allow user to the input string in program.
  • Read string from left to right.
  • Push each element/character in stack.
  • Once the string reading is done.
  • Pop each character one by one and put them back to the string.
  • Once the stack is empty return the result in string form.
  • Print the result
class  Stack_to_reverse  :
    # Creates  an  empty  stack.
    def	__init__(  self  ):
        self.items  =  list()
        self.size=-1

    #Returns  True  if  the  stack  is  empty  or  False  otherwise.
    def  isEmpty(  self  ):
        if(self.size==-1):
            return True
        else:
            return False

    # Removes  and  returns  the  top  item  on  the  stack.
    def  pop(  self  ):
        if  self.isEmpty():
            print("Stack is empty")
        else:
            return self.items.pop()
            self.size-=1

    # Push  an  item  onto  the  top  of  the  stack.
    def  push(  self,  item  ):
        self.items.append(item)
        self.size+=1

    def reverse(self,string):
        n = len(string)

 # Push all characters of string to stack
        for i in range(0,n):
            S.push(string[i])

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

 # Pop all characters of string and put them back to string

        for i in range(0,n):
            string+=S.pop()
        return string

S=Stack_to_reverse()
seq=input("Enter a string to be reversed")
sequence = S.reverse(seq)
print("Reversed string is " + sequence)

After executing the program, the output will be:

Enter a string to be reversed hello
Reversed string is olleh

Recommended Python Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *