Uses String::Builder

Reverse String
Reverse String in Crystal
module ReverseString
  def self.reverse(input : String) : String
    String.build(input.size) do |builder|
      (0...input.size).reverse_each do |i|
        builder << input[i]
      end
    end
  end
end

The method takes a String as an argument and returns a String. The constructor, String.build, takes the size of the string as an argument. The String.build method is from the String::Builder class. The String::Builder allows writing to a buffer in memory.

The constructor is given the size of the string to be reversed. This is due to the fact that we already know the size of the string. This is more efficient than using the default size of 16 and then resizing the string later.

A Range is created from 0 to the size of the string (exclusive). The reverse_each method iterates over the range in reverse order. So the last index of the string is the first value given to the block. For each index, the character at that index is appended to the string builder.

The String.build method is what is implicitly returned from the method.

11th Dec 2024 · Found it useful?