Last active
August 19, 2022 16:32
-
-
Save Yumiisa/74411b85852a9740c4f1a89481d8babd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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