-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCifradoCesar.java
42 lines (35 loc) · 1.7 KB
/
CifradoCesar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @author viticlick (viticlick@gmail.com)
*/
public class CifradoCesar {
private static short unicodeAmayuscula=65;
private static short unicodeAminuscula=97;
private static short tamañoAlfabeto = 26;
public static void main(String[] args) {
String frase = "Todo lo que se preguntaba eran las mismas respuestas que buscamos el resto de nosotros. ¿De dónde vengo? ¿A dónde voy? ¿Cuánto tiempo tengo? Todo lo que pude hacer fue sentarme y ver como moría.";
System.out.println( "cifrado: " + cifrador( frase , 3 ) );
}
private static String cifrador( String cadena , int n ){
/**
* para traducir solo minúsculas solo hay que añadir:
* cadena = cadena.toLowerCase();
*/
String toret="";
//Recorrido de la cadena
for( int i = 0 ; i < cadena.length() ; i++ ){
//Sustitución mayúsculas
if( cadena.codePointAt(i) >= unicodeAmayuscula &&
cadena.codePointAt(i) <= ( unicodeAmayuscula + tamañoAlfabeto ) ){
toret += (char) ( ( cadena.codePointAt( i ) - unicodeAmayuscula + n ) % tamañoAlfabeto + unicodeAmayuscula ) ;
//Sustitución minúsculas
}else if( cadena.codePointAt(i) >= unicodeAminuscula &&
cadena.codePointAt(i) <= ( unicodeAminuscula + tamañoAlfabeto ) ){
toret += (char) ( ( cadena.codePointAt( i ) - unicodeAminuscula + n ) % tamañoAlfabeto + unicodeAminuscula ) ;
}else{
//Resto de letras
toret += cadena.charAt( i );
}
}
return toret; //Retorna la cadena resultado
}
}