Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active July 8, 2021 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/78e5cd108e9b5b26d11f6ab21b1246f5 to your computer and use it in GitHub Desktop.
Save parzibyte/78e5cd108e9b5b26d11f6ab21b1246f5 to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ Si necesitas ayuda, contáctame en \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Creado por Parzibyte (https://parzibyte.me).
------------------------------------------------------------------------------------------------
| IMPORTANTE |
Si vas a borrar este encabezado, considera:
Seguirme: https://parzibyte.me/blog/sigueme/
Y compartir mi blog con tus amigos
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1
Twitter: https://twitter.com/parzibyte
Facebook: https://facebook.com/parzibyte.fanpage
Instagram: https://instagram.com/parzibyte
Hacer una donación vía PayPal: https://paypal.me/LuisCabreraBenito
------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#define LONGITUD 5 // Longitud de la matriz
#define MAXIMA_LONGITUD_CADENA 1000 // Límite de cadenas
char codigoGlobal = ' ';
/*
Prototipos de funciones
*/
void leeCodigo(char matriz[LONGITUD][LONGITUD]);
void leeTexto(char destino[MAXIMA_LONGITUD_CADENA]);
void codifica(char texto[MAXIMA_LONGITUD_CADENA], char matriz[LONGITUD][LONGITUD]);
void decodifica(char texto[MAXIMA_LONGITUD_CADENA], char matriz[LONGITUD][LONGITUD]);
void muestraMatriz(char matriz[LONGITUD][LONGITUD]);
void muestraTexto(char texto[MAXIMA_LONGITUD_CADENA]);
// Ayudantes:
int longitudCadena(char cadena[MAXIMA_LONGITUD_CADENA]);
int buscarFila(char matriz[LONGITUD][LONGITUD], char busqueda);
int buscarColumna(char matriz[LONGITUD][LONGITUD], char busqueda);
char remplazoPrimera(char matriz[LONGITUD][LONGITUD], char primeraLetra, char segundaLetra);
char remplazoSegunda(char matriz[LONGITUD][LONGITUD], char primeraLetra, char segundaLetra);
void consumirNuevaLinea(void);
int main()
{
char matriz[LONGITUD][LONGITUD];
leeCodigo(matriz);
printf("Matriz generada: \n");
muestraMatriz(matriz);
char texto[MAXIMA_LONGITUD_CADENA] = "";
char textoCodificado[MAXIMA_LONGITUD_CADENA] = "";
char textoDecodificado[MAXIMA_LONGITUD_CADENA] = "";
int eleccion;
printf("1. Codificar\n2. Decodificar\nElige [1-2]: ");
scanf("%d", &eleccion);
if (eleccion == 1)
{
printf("Voy a codificar un texto. Recuerda terminarlo con un *\n");
leeTexto(texto);
// Copiar el texto dentro del codificado para no modificar el original
strcpy(textoCodificado, texto);
// Codificarlo
codifica(textoCodificado, matriz);
printf("Texto codificado: \n");
muestraTexto(textoCodificado);
}
else
{
printf("Voy a decodificar un texto. Recuerda terminarlo con un *\n");
leeTexto(texto);
// Copiar el texto dentro del decodificado para no modificar el original
strcpy(textoDecodificado, texto);
// Codificarlo
decodifica(textoDecodificado, matriz);
printf("Texto decodificado: \n");
muestraTexto(textoDecodificado);
}
}
int longitudCadena(char cadena[MAXIMA_LONGITUD_CADENA])
{
return strlen(cadena);
}
void muestraMatriz(char matriz[LONGITUD][LONGITUD])
{
int y;
for (y = 0; y < LONGITUD; y++)
{
int x;
for (x = 0; x < LONGITUD; x++)
{
char actual = matriz[y][x];
printf("| %c ", actual);
}
printf("|\n");
}
}
void leeCodigo(char matriz[LONGITUD][LONGITUD])
{
printf("Ingresa el codigo: ");
scanf("%c", &codigoGlobal);
char letra = 'A';
int y = 0, x = 0;
while (letra <= 'Z')
{
if (letra == codigoGlobal)
{
letra++;
}
// Comprobación extra para cuando el código es 'Z'
if (letra <= 'Z')
{
matriz[y][x] = letra;
}
x++;
letra++;
if (x >= LONGITUD)
{
x = 0;
y++;
}
if (y >= LONGITUD)
{
y = 0;
}
}
}
int buscarFila(char matriz[LONGITUD][LONGITUD], char busqueda)
{
int y;
for (y = 0; y < LONGITUD; y++)
{
int x;
for (x = 0; x < LONGITUD; x++)
{
char actual = matriz[y][x];
if (actual == busqueda)
{
return y;
}
}
}
return -1;
}
int buscarColumna(char matriz[LONGITUD][LONGITUD], char busqueda)
{
int y;
for (y = 0; y < LONGITUD; y++)
{
int x;
for (x = 0; x < LONGITUD; x++)
{
char actual = matriz[y][x];
if (actual == busqueda)
{
return x;
}
}
}
return -1;
}
char remplazoPrimera(char matriz[LONGITUD][LONGITUD], char primeraLetra, char segundaLetra)
{
int columnaPrimera = buscarColumna(matriz, primeraLetra);
int filaPrimera = buscarFila(matriz, primeraLetra);
int columnaSegunda = buscarColumna(matriz, segundaLetra);
int filaSegunda = buscarFila(matriz, segundaLetra);
char remplazoPrimera = matriz[filaPrimera][columnaSegunda];
return remplazoPrimera;
}
char remplazoSegunda(char matriz[LONGITUD][LONGITUD], char primeraLetra, char segundaLetra)
{
int columnaPrimera = buscarColumna(matriz, primeraLetra);
int filaPrimera = buscarFila(matriz, primeraLetra);
int columnaSegunda = buscarColumna(matriz, segundaLetra);
int filaSegunda = buscarFila(matriz, segundaLetra);
char remplazoSegunda = matriz[filaSegunda][columnaPrimera];
return remplazoSegunda;
}
void consumirNuevaLinea(void)
{
int c;
do
{
c = getchar();
} while (c != EOF && c != '\n');
}
void leeTexto(char destino[MAXIMA_LONGITUD_CADENA])
{
consumirNuevaLinea();
printf("Ingresa el texto: ");
fgets(destino, MAXIMA_LONGITUD_CADENA, stdin);
//Remover salto de línea
if ((longitudCadena(destino) > 0) && (destino[longitudCadena(destino) - 1] == '\n'))
{
destino[longitudCadena(destino) - 1] = '\0';
}
}
void codifica(char texto[MAXIMA_LONGITUD_CADENA], char matriz[LONGITUD][LONGITUD])
{
int longitud = longitudCadena(texto) - 1; // El -1 es para no tomar en cuenta el *
int x = 0;
while (x < longitud)
{
char primera = texto[x];
if (x + 1 < longitud)
{
char segunda = texto[x + 1];
if (primera != ' ' && segunda != ' ' && primera != codigoGlobal && segunda != codigoGlobal)
{
char nuevaPrimera = remplazoPrimera(matriz, primera, segunda);
char nuevaSegunda = remplazoSegunda(matriz, primera, segunda);
texto[x] = nuevaPrimera;
texto[x + 1] = nuevaSegunda;
x++;
}
}
x++;
}
}
void decodifica(char texto[MAXIMA_LONGITUD_CADENA], char matriz[LONGITUD][LONGITUD])
{
// Se usa la misma función
codifica(texto, matriz);
}
void muestraTexto(char texto[MAXIMA_LONGITUD_CADENA])
{
printf("%s\n", texto);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment