Use an array

Reverse String
Reverse String in Crystal
module ReverseString
  def self.reverse(input : String) : String
    result = Array(Char).new(input.size)
    input.each_char do |char|
      result.unshift(char)
    end
    result.join
  end
end

The method takes a String as an argument and returns a String. An empty array stores the reversed string which stores Char. The reason for using Char instead of String is because Char is lighter than String, and we only store one character at a time. The Array is given the size of the string to be reversed, this makes the array doesn't have to be resized later which is more efficient.

The each_char method iterates over each character in the string. For each character so are they unshifted into the array. The unshift method adds the character to the beginning of the array. This means the first character in the string will be the last character in the array.

The join method converts the array back to a string.

11th Dec 2024 · Found it useful?