Created
August 15, 2016 14:49
-
-
Save lkrych/8c3f806fd600b46d26144b638cbd9b92 to your computer and use it in GitHub Desktop.
LetterChanges
This file contains hidden or 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 LetterChanges(str) | |
lower = "abcdefghijklmnopqrstuvwxyz" | |
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
vowels = "aeiou" | |
newString = [] | |
stringarray = str.chars | |
stringarray.each do |letter| | |
if letter.match(/^[[:alpha:]]$/) | |
index = lower.index(letter) | |
if vowels.include?(letter) | |
newString << (upper[index+1]) | |
else | |
newString << (lower[index + 1]) | |
end | |
else | |
newString << letter | |
end | |
end | |
newString.join | |
return newString | |
end | |
LetterChanges("Argument goes here") |
This file contains hidden or 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 LetterChanges(str) | |
# we will replace every letter in the string with the letter following it | |
# by first getting the number representation of the letter, adding 1 to it, | |
# then converting this new number to a letter using the chr function | |
# we also check to see if the character is z and if so we simply convert the z to an a | |
converted = str.gsub(/[a-zA-Z]/) do |c| | |
if c == 'z' or c == 'Z' | |
'a' | |
else | |
(c.ord + 1).chr | |
end | |
end | |
# we have now successfully converted each letter to the letter following it | |
# in the alphabet, and all we need to do now is capitalize the vowels | |
return converted.tr!('aeiou', 'AEIOU') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment