write a program to implement reversal of a string using stack data structure
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)
Comments
Leave a comment