Skip to content

Instantly share code, notes, and snippets.

@ivanjimenez
Created June 8, 2020 10:12
Show Gist options
  • Save ivanjimenez/5fa055e9cc8ab8f8ae75f6a7be2d6937 to your computer and use it in GitHub Desktop.
Save ivanjimenez/5fa055e9cc8ab8f8ae75f6a7be2d6937 to your computer and use it in GitHub Desktop.
ejercicios
import java.util.Scanner;
public class CifradoCesar2 {
static String abecedariomin = "abcdefghijklmnopqrstuvwxyz ";
static String abecedariomax = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
static int posicion;
public static String obtenerTexto(String texto) {
Scanner entrada = new Scanner(System.in);
System.out.println("introduzca el texto a cifrar: ");
texto = entrada.next();
System.out.println("introduce cuantas posiciones quieres rotarlo: ");
posicion = entrada.nextInt();
System.out.println(texto);
return texto;
}
public static void cifrarTexto(String texto) {
String textoCambiado = "";
System.out.println(CifradoCesar2.abecedariomax.length());
for (int i = 0; i < texto.length(); i++) {// me recorro el texto q quiero cifrar
for (int j = 0; j < abecedariomin.length(); j++) {// recorro las variables donde estan las letras
if (texto.charAt(i) == abecedariomin.charAt(j)) {// compruebo si cada posicion de mi texto esta en la
// variable de las letras
if (j + posicion >= abecedariomin.length()) {
textoCambiado += abecedariomin.charAt((j + posicion) % abecedariomin.length());
} // supongamos q esta en z,es el final
//si la posicion de j + posicion es mayor q la longitud de abecedario
} else {
textoCambiado += (abecedariomin.charAt((j + posicion) % abecedariomin.length()));
}
if (texto.charAt(i) == abecedariomax.charAt(j)) {// compruebo si cada posicion de mi texto esta en la
// variable de las letras
if (j + posicion >= abecedariomax.length()) {
textoCambiado += abecedariomax.charAt((j + posicion) % abecedariomax.length());
} // supongamos q esta en z,es el final
//si la posicion de j + posicion es mayor q la longitud de abecedario
} else {
textoCambiado += abecedariomax.charAt((j + posicion) % abecedariomin.length());
}
}
}
System.out.println(textoCambiado);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Scanner entrada = new Scanner(System.in);
String textoCodificado = new String();
textoCodificado = CifradoCesar2.obtenerTexto(textoCodificado);
cifrarTexto(textoCodificado);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment