(Coderbyte) Letter Changes - Solução

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.

Solução:

import java.util.*;
import java.io.*;

class Function {
  String LetterChanges(String str) {
    final String LETRAS = "abcdefghijklmnopqrstuvwxyz";
    final String LETRASN = "bcdefghijklmnopqrstuvwxyza";
    final String LETRASMAIUSC = LETRAS.toUpperCase();
    final String LETRASNMAIUSC = LETRASN.toUpperCase();
    final String VOGAIS = "aeiou";
    final String VOGAISMAIUSC = "AEIOU";
  
    String res = "";
    for (int i = 0; i < str.length(); i++) {
      if (LETRAS.indexOf(str.charAt(i)) > -1) {
        char letra = LETRASN.charAt(LETRAS.indexOf(str.charAt(i)));
        for (int j = 0; j < VOGAIS.length(); j++) {
          if (letra == VOGAIS.charAt(j)) {
           letra = VOGAISMAIUSC.charAt(j);
          }
        }
      
        res += letra;
      }
      else if (LETRASMAIUSC.indexOf(str.charAt(i)) > -1) {
        char letra = LETRASNMAIUSC.charAt(LETRASMAIUSC.indexOf(str.charAt(i)));
        for (int j = 0; j < VOGAIS.length(); j++) {
          if (letra == VOGAIS.charAt(j)) {
           letra = VOGAISMAIUSC.charAt(j);
          }
        }
       
        res += letra; 
      }
      else {
           res += str.charAt(i);
      }
    }
    return res;
  
  }

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

}           

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução