forked from seeditsolution/pythonprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse a string
More file actions
77 lines (63 loc) · 1.61 KB
/
Copy pathReverse a string
File metadata and controls
77 lines (63 loc) · 1.61 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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))