Letter Changes Coderbyte Solution

0

Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a).

Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

letter Changes coderbyte java


import java.util.Scanner;

public class LetterChanges {
final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
final String ALPHABET_UPPER = ALPHABET.toUpperCase();
final String VOWELS = "aeiou";

String LetterChanges(String str) {
StringBuilder newStr = new StringBuilder("");
for (char c : str.toCharArray()) {
int index = ALPHABET.indexOf(c);
if (index > -1) {
char newChar = index < ALPHABET.length() - 1 ? ALPHABET
.charAt(index + 1) : 'a';
if (VOWELS.indexOf(newChar) > -1) {
newChar = Character.toUpperCase(newChar);
}
newStr.append(newChar);
} else {
index = ALPHABET_UPPER.indexOf(c);
if (index > -1) {
char newChar = index < ALPHABET_UPPER
.length() - 1 ? ALPHABET_UPPER.charAt(index + 1) : 'A';
newStr.append(newChar);
} else {
newStr.append(c);
}
}
}

return newStr.toString();
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
LetterChanges c = new LetterChanges();
System.out.print(c.LetterChanges(s.nextLine()));
}
}


Letter Changes I coderbyte python

Using the Python language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm.  Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string. 


def LetterChanges(str):
result = ""

for x in str:
if x == "z":
newChar = "a"
elif x == "Z":
newChar = "A"
elif x.isalpha():
newChar = chr(ord(x) + 1)
else:
newChar = x

if newChar in "aeiou":
newChar = newChar.upper()
result = result + newChar

return result


Letter Changes I coderbyte JavaScript

Using the JavaScript language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string. 


function LetterChanges(str) {

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var newStr = "";
var loc;

for (var i = 0; i < str.length; i++) {
loc = alphabet.indexOf(str[i]);
if (loc === 25) {
newStr = newStr + "a";
} else if (loc === -1) {
newStr = newStr + str[i];
} else {
newStr = newStr + alphabet[loc + 1];
}
}
return newStr.replace(/[aeiou]/g, function(letter) {return letter.toUpperCase()});
}

Explanation

You have to realize that the string passed in may contain items other than letters of the alphabet. If you find a character that is not a-z then just pass it along to the newStr as is without any modification.  I am going to compare each letter in the string to the alphabet string. If the letter is found then I am going to return the next letter in the string unless the letter is z and them I am going to return a. When finished I am going to use a RegExp to replace all lower case vowels with upper case.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !