|
| 1 | +package easy; |
| 2 | + |
| 3 | +/** |
| 4 | + * Have the function LetterChanges(str) take the str parameter |
| 5 | + * being passed and modify it using the following algorithm. |
| 6 | + * Replace every letter in the string with the letter |
| 7 | + * following it in the alphabet (i.e. c becomes d, z becomes a). |
| 8 | + * Then capitalize every vowel in this new string (a-e-i-o-u) |
| 9 | + * and finally return this modified string. |
| 10 | + */ |
| 11 | +public class LetterChanges { |
| 12 | + |
| 13 | + |
| 14 | + /** |
| 15 | + * Letter Changes function. |
| 16 | + * |
| 17 | + * @param str input string |
| 18 | + * @return modified string |
| 19 | + */ |
| 20 | + private static String letterChanges(String str) { |
| 21 | + |
| 22 | + char[] alphabet = {'b', 'c', 'd', 'E', 'f', 'g', 'h', 'I', 'j', 'k', 'l', |
| 23 | + 'm', 'n', 'O', 'p', 'q', 'r', 's', 't', 'U', 'v', 'w', 'x', 'y', 'z', 'A'}; |
| 24 | + char[] charArray = str.toLowerCase().toCharArray(); |
| 25 | + StringBuilder output = new StringBuilder(); |
| 26 | + |
| 27 | + for (int i = 0; i < str.length(); i++) { |
| 28 | + char letter = str.charAt(i); |
| 29 | + boolean isLetter = letter >= 'a' && letter <= 'z'; |
| 30 | + if (isLetter) { |
| 31 | + output.append(alphabet[charArray[i] - 97]); |
| 32 | + } else { |
| 33 | + output.append(letter); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return output.toString(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Entry point. |
| 42 | + * |
| 43 | + * @param args command line arguments |
| 44 | + */ |
| 45 | + public static void main(String[] args) { |
| 46 | + var result1 = letterChanges("anthology"); |
| 47 | + System.out.println(result1); |
| 48 | + var result2 = letterChanges("equilibrium"); |
| 49 | + System.out.println(result2); |
| 50 | + var result3 = letterChanges("oxford"); |
| 51 | + System.out.println(result3); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments