Encrypt a phrase/word by reversing and switching vowels with the following vowels

Asked

Viewed 1,019 times

9

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

  • reverse the word
  • where there is a vowel, replace with the next vowel

And display the encrypted word.

I know how to do the word reversal part, but replacing vowels I’ve tried several ways and I couldn’t. 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();
    }

}
  • This range from 1 to 20 is the word size?

  • That’s why I put the while one

  • Do you have any example of how the final output of the program should look? Example, if I feed with the word abcei, what kind of output it should generate?

1 answer

3


This is just a pseudo encryption. The code has some problems and is a bit confusing. I modified it to get 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)));
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that I have made the methods self-contained, which is ideal except when there really is a different need. I also separated user interface logic (data input or output) from encryption logic. You can make some cosmetic improvements to the interface.

Because it is a relatively complex operation and clearly different from the rest I separated the vowel exchange in a method of its own.

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

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

It tries to find out if the character being analyzed is a vowel (uppercase or lowercase). If it is not, it just returns the character. If it is it takes the next vowel (sum 2 because the uppercase and lowercase are interspersed. There is an exception when the vowel is U or u, Then I guess, take the first one, that’s what I did.

Note that I did not worry about accents and other peculiarities, I think this simplification will not bring problem in an exercise. It’s probably desirable that it’s really simple.

In a general way the code is simpler, without redundancies 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.

  • The part of the title that speaks "cryptography" reminds me of a company that u worked where the administration login systems were all "encrypted" using the following logic: 4dm1n1str4c40 , suc3ss0 and so on, wouldn’t it be interesting to change the title of the question to "Change a text/phrase..."

  • This title could generate misinterpretation for future users

  • @Marcelobonifazio I do not know if it is necessary, it was what he wanted to ask. Maybe other people look for the same. People use the wrong terms for things, if you take it off it can get harder to find, even if it’s strictly wrong to talk in cryptography. That is why I make the exception in my reply. Reading you will see that something is wrong. These comments make it even clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.