Solutions for Letter Changes
View the challenge

Official solution

This challenge requires you to change every letter in the string to the letter following it in the alphabet, so a becomes b, z becomes a, etc. Once every letter is changed, we then need to capitalize only the vowels, namely: a, e, i, o, u. We will be changing the letters by using their respective ASCII values and adding 1 to them in order to get the next letter in the alphabet. ASCII values are just numbers that are assigned to each character in a sequential order, for example, the ASCII code for the letter a is 97 and b is 98. What we'll do is convert a letter to its ASCII number, add 1 to it, then convert this new number back into a character using a built-in character function.

``````function LetterChanges(str) {

// we will replace every letter in the string with the letter following it
// by first getting the charCode number of the letter, adding 1 to it, then
// converting this new charCode number to a letter using the fromCharCode function
// we also check to see if the character is z and if so we simply convert the z to an a
var converted = str.replace(/[a-z]/gi, function(char) {
return (char === 'z' || char === 'Z') ? 'a' : String.fromCharCode(char.charCodeAt() + 1);
});

// 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
var capitalized = converted.replace(/a|e|i|o|u/gi, function(vowel) {
return vowel.toUpperCase();
});

// return the final string
return capitalized;

}

LetterChanges("fun times!");
``````
``````def LetterChanges(str):

# our new string with the modified characters
newString = ""

# begin by looping through each character in the string
for char in str:

# check if the current character is an alphabetic character
if char.isalpha():

# check if character is z
if char.lower() == 'z':
char = 'a'

# if alphabetic character then add 1 to its ASCII value
# by using the built-in ord function then convert back to character
else:
char = chr(ord(char) + 1)

# if new character is a vowel then capitalize it
if char in 'aeiou':
char = char.upper()

# add this modified character to the new string
newString = newString + char

return newString

print LetterChanges("fun times!")
``````
``````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

# keep this function call here
puts LetterChanges(STDIN.gets)
``````

Top Rated User Solution

``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
Top User Solutions