How do I make a program to invert a password by ignoring the # and _?

Asked

Viewed 97 times

0

I have an exercise to do with this theme but I can’t reverse the second part after a special character.

Example: teste_baixo#123 would be traded for etset_oxiab#321.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        PilhaInt pilha = new PilhaInt();

        pilha.init();

        Scanner teclado = new Scanner(System.in);

        String senha = new String();
        String salva = new String();

        System.out.println("insira senha= ");
        senha = teclado.nextLine();

        for (int i = 0; i < senha.length(); i++) {
            if (senha.charAt(i) != '_' && senha.charAt(i) != '#') {
                pilha.push(senha.charAt(i));
            }else{
                for (int j = i-1; j >= 0; j--) {
                    System.out.printf("%c",senha.charAt(j));
                }
                System.out.println(senha.charAt(i));
            }
            System.out.println(pilha.topo);
        }
    }
}

1 answer

1


You didn’t put the class code PilhaInt, but let’s assume that it is complete and has a method pop, which pops the top element and returns it, and which also has a method to check if it is empty. So the idea of the algorithm is to stack the characters until you find one _ or #.

In case, I stack the t, then the e, then the s, etc... And when finding the character _, the pile will be like this:

e  <-- topo
t
s
e
t

Upon finding the _, Just pop these elements and add them in the new password, they will already be placed in reverse order (since the destacking starts from the element to the top). Do this until the stack is empty. Then you add the stack itself _ in the new password and continues the loop.

Would look like this:

String senha = "teste_baixo#123";
String novaSenha = "";
for (int i = 0; i < senha.length(); i++) {
    char c = senha.charAt(i);
    if (c != '_' && c != '#') {
        pilha.push(c);
    } else { // é "_" ou "#", esvazia a pilha
        while (!pilha.isVazia()) {
            novaSenha += pilha.pop();
        }
        novaSenha += c; // adiciona o _ ou #
    }
}
// pode ter sobrado alguma coisa na pilha
while (!pilha.isVazia()) {
    novaSenha += pilha.pop();
}
System.out.println(novaSenha); // etset_oxiab#321

I used string concatenation with +, but you could also use a StringBuilder, for in loops it is more efficient than using +.

You can also delete the block repetition while creating a specific method for that section:

private void esvaziaPilha(Pilha pilha, StringBuilder novaSenha) {
    while (!pilha.isVazia()) {
        novaSenha.append(pilha.pop());
    }
}

...
String senha = "teste_baixo#123";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < senha.length(); i++) {
    char c = senha.charAt(i);
    if (c != '_' && c != '#') {
        pilha.push(c);
    } else { // é _ ou #, esvazia a pilha
        esvaziaPilha(pilha, sb);
        sb.append(c); // adiciona o _ ou #
    }
}
esvaziaPilha(pilha, sb);
String novaSenha = sb.toString();
  • 1

    magnificent face, full and clear explanation, thank you very much!!

Browser other questions tagged

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