Skip to content

Instantly share code, notes, and snippets.

@dannluciano
Created December 7, 2021 19:39
Show Gist options
  • Save dannluciano/fb3fba7f10f5db51fa49e72dfc29d450 to your computer and use it in GitHub Desktop.
Save dannluciano/fb3fba7f10f5db51fa49e72dfc29d450 to your computer and use it in GitHub Desktop.
ROT13
import java.util.Scanner;
public class Rot13 {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
char[] mapa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
int chave = 13;
System.out.println("ROT13");
while(true){
System.out.println("Digite uma mensagem:");
String mensagemOriginal = teclado.nextLine().toUpperCase();
String mensagemProcessada = "";
for (int i = 0; i < mensagemOriginal.length(); i++) {
char letra = mensagemOriginal.charAt(i);
boolean eUmCaractere = false;
int indice = 0;
while (indice < mapa.length) {
if (letra == mapa[indice]){
eUmCaractere = true;
break;
}
indice++;
}
if (eUmCaractere) {
int indiceChave = (indice+chave) % mapa.length;
mensagemProcessada += mapa[indiceChave];
} else {
mensagemProcessada += letra;
}
}
System.out.println("-".repeat(40));
System.out.println(mensagemProcessada);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment