Encrypt a phrase / word by reversing and changing the vowels, by the following vowels

7

I was given an exercise in which I have to write a program that reads a word in the range [1-20] and encrypts this word using the steps:

  • Reverse the word
  • where you have a vowel, replace with the next vowel

And display the word encrypted.

I can do the reverse part of the word, but replacing the vowels I've tried in several ways and I could not. Here's what I've been able to do so far:

package ExerciciosPackage02;
import java.util.Scanner;

public class ExerciciosClass01h {
    static Scanner ler=new Scanner(System.in);
    static String palavra;
    public static void lerpalavra(int ini, int fim){
        System.out.println("Digite uma palavra:");
        do{
            System.out.print("-> ");
            palavra=ler.nextLine();
        }while ((palavra.length()<ini)||(palavra.length()>fim));
    }
    public static void inverter(){
        int ctcar;
        System.out.print("Criptografia: ");
        for(ctcar=palavra.length()-1;ctcar>=0;ctcar--){
            System.out.print(palavra.charAt(ctcar));
        }
    }
    public static void invertvogal(){
        //int ctcar;
        //System.out.println(palavra.replaceAll("a", "e"));
        //for(ctcar=palavra.length()-1;ctcar>=0;ctcar--){
        //}
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        lerpalavra(1,20);
        inverter();
        invertvogal();
    }

}
    
asked by anonymous 30.05.2016 / 00:58

1 answer

3

This is just a pseudo-encryption. The code has some problems and is a bit confusing. I've modified it to stay more organized:

import java.util.Scanner;

class ExerciciosClass01h {
    public static String lerpalavra(int ini, int fim) {
        Scanner ler = new Scanner(System.in);
        System.out.println("Digite uma palavra:");
        String palavra;
        do {
            System.out.print("-> ");
            palavra = ler.nextLine();
        } while (palavra.length() < ini || palavra.length() > fim);
        return palavra;
    }
    public static String criptografar(String palavra) {
        String invertida = "";
        for (int ctcar = palavra.length() - 1; ctcar >= 0; ctcar--) {
             invertida += trocaVogal(palavra.charAt(ctcar));
        }
        return invertida;
    }
    public static char trocaVogal(char letra) {
        String vogais = "AaEeIiOoUu";
        int posicao = vogais.indexOf(letra);
        if (posicao == -1) {
            return letra;
        }
        return vogais.charAt(posicao + 2);
    }
    public static void main(String[] args) {
        System.out.println(criptografar(lerpalavra(1, 20)));
    }
}

See running on ideone and on CodingGround .

Note that I made the methods self-contained, which is ideal unless there really is a different need. I also separated user interface logic (enter or output data) from the encryption logic. You can do some cosmetic improvements on the interface.

Because it is a relatively complex operation and distinctly different from the rest, I've separated the vowel into a method of its own.

There are some things that could be better, but for simple code it's not worth it. An example would be the concatenation of the characters being done with a StringBuiler for performance reasons. But let's not complicate this example that does not depend on the best performance possible.

Other logics could have been made to change the vowel, maybe even better, but that was the first one I thought.

It tries to find out if the character being parsed is a vowel (uppercase or lowercase). If it is not, just return the character. There is an exception when the vowel is U or u , so, I guess, take the first one, that's what I did.

Note that I did not bother with accents and other quirks, I think this simplification will not bring trouble in an exercise. It is probably desirable that it be simple indeed.

In general, the code is simpler, no redundancy, and more readable, even by the best use of spacing. I could have improved the names of the variables, but I preferred not to touch it.

    
30.05.2016 / 01:36