Home » Python » Python programs

Python program to reverse a string using stack and reversed method

Here, we are going to learn how to reverse a string by using stack and reversed method in python?
Submitted by Sanjeev, on April 11, 2019

Given a string and we have to reverse it by using stack and by using reversed method in python.

1) Reverse a string by using stack

Procedure:

  1. First create an empty stack
  2. Push each character one by one in stack
  3. Pop each character one by one and put them back to the string

2) Reverse a strung using reversed() method

In this method, we will use reversed() method and iterate over the reversed iterator to get the reversed string.

Python code to reverse a string

import sys

def push(element, size, stack):
    '''
    this function is used to push the elements
    in the stack and it will return Error! message
    if the stack is full and terminate the program.
    '''
    global top
    if top >= size - 1:
        print('Stack Overflow')
        sys.exit()
    else:
        top += 1
        stack[top] = element

def pop():
    '''
    this function is used to pop elements from
    the stack and it will return Error! message
    if the stack is empty and terminate the program.
    '''
    global top
    if top < 0:
        print('Stack Underflow')
        sys.exit()
    else:
        element = stack[top]
        print('%s' % element, end='')
        top -= 1

def reverse_by_sort(string):
    '''
    This function is used to reverse any string
    by reversed() method.
    '''

    string = list(string)
    rev_str = ''
    
    for i in reversed(string):
        rev_str += i

    return rev_str

if __name__=='__main__':

    size = 11
    stack = [0]*size
    string = 'Includehelp'
    top = -1

    # Pushing value in the stack
    push('I', 11, stack)
    push('n', 11, stack)
    push('c', 11, stack)
    push('l', 11, stack)
    push('u', 11, stack)
    push('d', 11, stack)
    push('e', 11, stack)
    push('h', 11, stack)
    push('e', 11, stack)
    push('l', 11, stack)
    push('p', 11, stack)

    print('Original String = %s' % string)
    
    print('\nUsing Stack')

    # Popping values from stack and printing them
    print('Reversed String = ',end='')
    for i in stack:
        pop()

    print('\n\nUsing sort()')
    print('Reversed string = %s' % reverse_by_sort(string))

Output

Original String = Includehelp

Using Stack
Reversed String = plehedulcnI

Using sort()
Reversed string = plehedulcnI





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.