Program in python for reversing a string using stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #Function for creating stack def createstack(): stack=[] return stack #Function for pushing onto stack def push(stack,item): stack.append(item) #Function for popping an item def pop(item): if len(stack)==0: return 0 return stack.pop() #function for reverse string using stack def reverse(string): n=len(string) stack=createstack() #Push all characters of string on to stack for i in range(n): push(stack,string[i]) #Making string empty since all characters are on stack string="" for i in range(len(stack)): string =string+pop(stack) return string #Main program for reversing string string="ArtofAlgorithm" string=reverse(string) print string |