koniec
0
2
Mnie boli to, że wrzucasz kod, którego nie próbowałeś nawet uruchomić.
- W c nie ma czegoś takiego jak string -> unknown type name 'string'
- GaDeRy( stringtoencode, GADERYPOLUKIgaderypoluki ); to się nie wykona -> GADERYPOLUKIgaderypoluki undeclared
- printf("Wynik:\n", stringtoencode); -> ta linijka nic nie wyświetli
- Po wybraniu jednej opcji wchodzimy w nieskończonego while'a.
0
#include <stdio.h>
#include <stdlib.h>
#define MSG_SIZE 20
//na potrzeby programu moja wlasna implementacja
int strlen(char* str)
{
int len = 0;
while(str[len] != 0)
{
++len;
}
return len;
}
//wyczyszczenie szyfru z myslnikow
char* cleanCipher(char* cipher)
{
int i, j;
int cipherSize = strlen(cipher);
char* cleaned = (char*) calloc(cipherSize, sizeof(char));
j=0;
for(i = 0; i < cipherSize; ++i)
{
if(cipher[i] != '-')
{
cleaned[j] = cipher[i];
++j;
}
}
return cleaned;
}
char substitute(char letter, char* cipher)
{
int i;
int cipher_length = strlen(cipher);
char substitution = letter;
for(i = 0; i < cipher_length; ++i)
{
if(cipher[i] == letter)
{
substitution = cipher[(i%2) ? (i-1) : (i+1)];
break;
}
}
return substitution;
}
void GaDeRy(char* stringtoencode, char* fullcipher)
{
int i;
char* cipher = cleanCipher(fullcipher);
int string_length = strlen(stringtoencode);
for(i = 0; i < string_length; ++i)
{
stringtoencode[i] = substitute(stringtoencode[i], cipher);
}
}
int main()
{
char* cipher1 = "GA-DE-RY-PO-LU-KI";
char* cipher2 = "PO-LI-TY-KA-RE-NU";
char* cipher3 = "KA-CE-MI-NU-TO-WY";
char stringtoencode[MSG_SIZE] = {'0'};
int option;
printf("Enter a string that you want to encode:\nMax 20 characters\n");
scanf("%s", &stringtoencode);
printf("Choose the cipher\n");
printf("[1]GA-DE-RY-PO-LU-KI\n");
printf("[2]PO-LI-TY-KA-RE-NU\n");
printf("[3]KA-CE-MI-NU-TO-WY\n");
scanf("%d",&option);
switch(option)
{
case 1: GaDeRy(stringtoencode, cipher1 );
break;
case 2: GaDeRy(stringtoencode, cipher2 );
break;
case 3: GaDeRy(stringtoencode, cipher3 );
break;
}
printf("Encrypted as: %s\n", stringtoencode);
}
Uwagi do kodu:
- Jeśli wczytywany ma być tekst ze spacjami to trzeba zamienić scanf na fgets, albo użyć
character classes
. - Zamiast tego switcha można zrobić tablicę szyfrów i potem:
GaDeRy(stringtoencode, cipher[option] );