Answer to Question #267454 in Python for kavin

Question #267454

write a program to implement reversal of a string using stack data structure

1
Expert's answer
2021-11-17T06:30:49-0500
def push(stack: list, item: str):
    stack.append(item)


def pop(stack: list):
    return stack.pop()


def size(stack: list):
    return len(stack)


def reverse(string: str):
    stack = []
    for item in string:
        push(stack, item)
    string = ''
    for _ in range(0, size(stack)):
        string += pop(stack)
    return string


string = input('Enter string: ')
string = reverse(string)
print(string)

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