-
-
Save RickArora/37ae6a0352ad532fd37e29faf6769dec 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
# Return the middle character of a string. Return the middle two characters if | |
# the word is of even length, e.g. middle_substring("middle") => "dd", | |
# middle_substring("mid") => "i" | |
def middle_substring(str) | |
finalString = "" | |
if (str.length % 2) == 0 | |
x = str.chars | |
finalString += x[(x.(length/2).ceil] | |
finalString += x[x.(length/2).floor] | |
else | |
x = str.chars | |
finalString += x[x.(length/2).ceil] | |
end | |
return finalString | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment