You're out of free questions.

Upgrade now

Write a method that takes a string and reverses the letters in place.

In general, an in-place algorithm will require swapping elements.

We swap the first and last characters, then the second and second-to-last characters, and so on until we reach the middle.

def reverse!(str) left_index = 0 right_index = str.length - 1 while left_index < right_index # Swap characters. str[left_index], str[right_index] = \ str[right_index], str[left_index] # Move towards middle. left_index += 1 right_index -= 1 end end

time and space.

Reset editor

Powered by qualified.io

. . .