Skip to content

Instantly share code, notes, and snippets.

@Yumiisa
Last active August 19, 2022 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yumiisa/74411b85852a9740c4f1a89481d8babd to your computer and use it in GitHub Desktop.
Save Yumiisa/74411b85852a9740c4f1a89481d8babd to your computer and use it in GitHub Desktop.
def reverse_str(str)
reversed_string=''
i =0
while i < str.length
reversed_string=str[i] + reversed_string # reverse
i +=1
end
reversed_string
end
puts reverse_str("Hello")
#
def reverse(string)
#base case where we return the string if its length is one since you cant reverse string that has length of less than one
if string.length==1
string
else
#recursion case, where we will slice through the string to split it then concatenate the sliced string at the end of substring
reverse(string[1..string.length]) +string[0]
end
end
#calling the function
#string=gets.chomp
puts reverse("Gaudencia")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment