-
-
Save matosdiego/3407d18b75584e32e5af45d7be0960c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 06-6-string-ROT13.c | |
| Tratamiento de strings. | |
| Aplica ROT13 a una cadena de caracteres. | |
| */ | |
| #include <stdio.h> | |
| char rot13(char caracter); | |
| void main() { | |
| // Definición de cadena de carácteres fija | |
| char mensaje[] = "Esto es un mensaje que se va a cifrar con ROT13."; // al final hay \0 (null) | |
| char cifrado[sizeof(mensaje)]; | |
| int c = 0; // contador de caracteres | |
| char cr13; // caracter cifrado | |
| // Recorre la cadena de caracteres a cifrar | |
| while (mensaje[c] != '\0') { | |
| cr13 = rot13(mensaje[c]); // convierte a rot13 | |
| if (cr13 != 0) { | |
| cifrado[c] = cr13; // guarda el caracter cifrado | |
| } else { | |
| cifrado[c] = mensaje[c]; // si no es un caracter cifrable, lo copia tal cual | |
| } | |
| c++; // incrementa indice (siguiente caracter) | |
| } | |
| cifrado[c] = '\0'; // añade el caracter de fin de cadena | |
| printf("\nLa cadena original es\n\n[ %s ]\n\n", mensaje); | |
| printf("\nLa cadena cifrada es\n\n[ %s ]\n\n", cifrado); | |
| return; | |
| } | |
| /* | |
| Recibe una caracter del alfabeto a-z, A-Z y devuelve el | |
| caracter correspondiente en el alfabeto rot13 | |
| */ | |
| char rot13(char caracter) { | |
| char alfabeto_normal[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| char alfabeto_rot13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; | |
| int c = 0; // contador de caracteres | |
| // Buscar el caracter en el alfabeto normal y si lo | |
| // encuentra devuelve el correspondiente en el alfabeto rot13 | |
| while (alfabeto_normal[c] != '\0') { | |
| if (alfabeto_normal[c] == caracter) { | |
| return alfabeto_rot13[c]; | |
| } | |
| c++; // incrementa indice (siguiente caracter) | |
| } | |
| return 0; // si no lo encuentra devuelve 0 | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment