Advertisement
brendan-stanford

rev_string

Aug 16th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #Create an empty list to store the letters of the string
  2. letters = []
  3.  
  4. #Define a function that collects the letters of a string and appends them into a list
  5. def collect_string():
  6. word = input("word to reverse:")
  7. for i in word:
  8. letters.append(i)
  9. return letters
  10.  
  11. #Create an empty list to store each letter from last to first
  12. def rev_string(string):
  13. rev = []
  14. size = len(letters)
  15. for i in letters:
  16. if size > 0:
  17. rev.append(letters[size-1])
  18. size -= 1
  19.  
  20. #at the end of the function, use the join method to concatenate the reversed list back into a string; "" specifies no gaps between list items
  21. back = "".join(rev)
  22. return back
  23.  
  24. #Call the function anf print the output
  25. print(rev_string(collect_string()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement