public
Last active

reverse a string without reverse method.

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.