Last active
August 9, 2021 19:47
-
-
Save Roberto24p/498329b2adfac39caf17e3de65feaf24 to your computer and use it in GitHub Desktop.
Cifrado
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define TAMANIOCLAVE 50 | |
#define TAMANIOTEXT 100 | |
struct columna{ | |
int claveAscii; | |
char *dataColumna; | |
}; | |
typedef struct columna COLUMNA; | |
int getSizeString(char *); | |
void stringNoSpaces(char *, int, char *); | |
void ordenarColumna(COLUMNA *, int); | |
int main(){ | |
COLUMNA *data = NULL; | |
char clave[TAMANIOCLAVE]; | |
char txt[TAMANIOTEXT]; | |
int sizeClave; | |
int sizeText; | |
printf("Ingresa la clave (MAX 50 CARACTERES): "); | |
fgets(clave, 50, stdin); | |
printf("Ingrese cadena a Cifrar (MAX 100 CARACTERES): "); | |
fgets(txt, 100, stdin); | |
sizeClave = getSizeString(clave); | |
sizeText = getSizeString(txt); | |
data = malloc(sizeof(COLUMNA)*sizeClave); | |
int sizeFIla = sizeText/sizeClave; | |
if((sizeText%sizeClave)!=0){ | |
sizeFIla++; | |
} | |
char *newTxt = malloc(sizeof(char *)* sizeText); | |
stringNoSpaces(txt, sizeText, newTxt); | |
for(int i = 0; i<sizeClave; i++){ | |
data[i].dataColumna = malloc(sizeof(char *)*sizeFIla); | |
data[i].claveAscii = clave[i]; | |
} | |
int abecedario = 97; | |
for(int i = 0, txtI=0; i<sizeFIla; i++){ | |
for(int j = 0; j<sizeClave; j++, txtI++){ | |
if(newTxt[txtI]==0 || newTxt[txtI]==10){ | |
data[j].dataColumna[i] = abecedario; | |
abecedario = abecedario+1; | |
}else{ | |
data[j].dataColumna[i] = newTxt[txtI]; | |
} | |
} | |
} | |
ordenarColumna(data, sizeClave); | |
printf("Cifrado-->"); | |
for(int i = 0; i<sizeClave;i++){ | |
printf("%s", data[i].dataColumna); | |
} | |
printf("\n"); | |
return 0; | |
} | |
void stringNoSpaces(char *string, int tamanio, char *newString){ | |
int tam = strlen(string); | |
for(int i = 0, j = 0; i<tam;i++, j++){ | |
if(string[i]==32){ | |
i++; | |
} | |
newString[j] = string[i]; | |
} | |
} | |
int getSizeString(char *string ){ | |
char *auxString = string; | |
int size = 0; | |
while(*auxString){ | |
if(*auxString!=' '){ | |
size++; | |
} | |
auxString++; | |
} | |
return size-1; | |
} | |
void ordenarColumna(COLUMNA *column, int tamanio){ | |
int flag = 0; | |
for(; flag != 1;){ | |
flag = 1; | |
for(int i = 0; i<tamanio; i++){ | |
if(column[i].claveAscii > column[i+1].claveAscii && (i+1)!=tamanio){ | |
COLUMNA auxCol = column[i]; | |
column[i] = column[i+1]; | |
column[i+1] = auxCol; | |
flag = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment