Created
September 30, 2020 03:18
-
-
Save Abhayparashar31/9c3c0dad38cf946b53d9c408ea391334 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
String : Python | |
reversed string : nohtyp | |
python | |
[p,y,t,h,o,n] | |
""+n = n | |
"n"+o = no | |
'nohtyp' | |
""" | |
class Stack(): | |
def __init__(self): | |
self.items = [] ## Empty list intilize | |
def push(self,item): | |
self.items.append(item) ## simple appending to list | |
def pop(self): | |
return self.items.pop() ## Removing top element of the stack | |
def is_empty(self): | |
return self.items==[] ### Checking whether the stack is empty or not | |
def peek(self): | |
if not self.is_empty(): | |
return self.items[-1] ## returnign top element using [-1]=last element | |
def show_stack(self): | |
return self.items ## Printing all the items | |
def reverse_strings(string): | |
s = Stack() ## object for stack class | |
for i in range(len(string)): ## Loop from 0 to len of string | |
s.push(string[i]) ## Push each character onto string | |
reverse_string = "" ## Create a empyt reverse string | |
while not s.is_empty(): ## while loop until the stack is empty or not | |
reverse_string += s.pop() ## pop top element and concatenate with the reverse string | |
return reverse_string ## Return reverse string | |
string = str(input("Enter a string : ")) ## Taking string as input | |
print(reverse_strings(string)) ## Printing reverse string as output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment