Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def reverse_string(input_string):
- stack = []
- reversed_string = ""
- # Push each character of the input string onto the stack
- for char in input_string:
- stack.append(char)
- # Pop characters from the stack to construct the reversed string
- while stack:
- reversed_string += stack.pop()
- return reversed_string
- # Example usage:
- input_str = "python"
- reversed_str = reverse_string(input_str)
- print(reversed_str) # Output: "nohtyp"
Advertisement
Add Comment
Please, Sign In to add comment