Created
July 5, 2012 18:17
-
-
Save chadjemmett/3055436 to your computer and use it in GitHub Desktop.
reverse a string without reverse method.
This file contains 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
# the variables. a forward string | |
#the backward empty string. | |
#the holding array | |
forwards = "hello this is a string. In a moment it will be reversed without using the 'reverse' method." | |
backwards = "" | |
holding_array = [] | |
#put the string in the array | |
forwards.each_char {|letter| holding_array.push(letter)} | |
#it's first in first out. We push the letters one by one into an array. then we pop them back out into the backwards string | |
holding_array.length.times {|letters| backwards << holding_array.pop} | |
puts forwards | |
puts backwards | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment