Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Reverse an string Using Stack in Python

def createStack():
stack=[ ]
return stack
def size(stack):
return len(stack)
def isEmpty(stack):
if size(stack) == 0:
return true
def push(stack,item):
stack.append(item)
def pop(stack):
if isEmpty(stack): return
return stack.pop()
def reverse(string):
n = len(string)
stack = createStack()
for i in range(0,n,1):
push(stack,string[i])
string=""
for i in range(0,n,1):
string+=pop(stack)
return string
s = "SoftHunt"
print ("The original string is : ",end="")
print (s)
print ("The reversed string(using stack) is : ",end="")
print (reverse(s))
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Reverse #string #Using #Stack #Python
ADD COMMENT
Topic
Name
6+5 =