Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct crypt{
char encoded; char decoded;
};
void end_game( char * );
void manual( struct crypt [] , char *, int );
void auto_guess( struct crypt [] , char * , char [] );
int main()
{
int i,j;
char *file, *message;
char ch, kiwi;
char freq[26]={'e','t','a','o','i','n','s','h','r','d','l','c','u','m','w','f','g','y','p','b','v','k','j','x','q','z'};
char name[80]={'\0'};
struct crypt key[] =
{{'a','-'},{'b','-'},{'c','-'},{'d','-'},{'e','-'},{'f','-'},{'g','-'},{'h','-'},{'i','-'},{'j','-'},{'k','-'},
{'l','-'},{'m','-'},{'n','-'},{'o','-'},{'p','-'},{'q','-'},{'r','-'},{'s','-'},{'t','-'},{'u','-'},{'v','-'},
{'w','-'},{'x','-'},{'y','-'},{'z','-'}};
FILE *fp;
printf("************************************************************************ \n");
printf("** WELCOME!!!! TO THE DECODER!!! ** \n");
printf("************************************************************************ \n \n");
printf("Would you like to play a game? (Y or N): ");
scanf("%c", &ch);
if( ch == 'N' || ch == 'n' )
{
printf("\nFarwell!! \n");
ch = getchar();
exit(0);
}
printf("Please input a filename where the encrypted message is contained: \n");
scanf("%s", name);
/* Now to find how long the file is, note: this will include carriage returns initially */
fseek( fp , 0 , SEEK_END );
i = ftell( fp ); /* i is length of file */
/* get file into dynamically allocated array of correct size */
file = (char *)calloc( i , sizeof(char) );
fseek( fp , 0 , SEEK_SET );
j = fread( file , sizeof(char) , i , fp );
message = (char *)calloc( j , sizeof(char) );
strcpy( message , file );
free( file );
/* Now the file is in the array, the key is initialized */
while( i != 3 )
{
printf("Encoded letter: ");
for( i=0 ; i<26 ; i++ )
{
printf(" %c", key[i].encoded);
}
printf("\n\nDecoded letter: ");
for( i=0 ; i<26 ; i++ )
{
printf(" %c", key[i].decoded);
}
printf("\n\n");
/* Apply messge based on current key */
/* so if dash, leave as is, if decodes to, then replace with CAP decoded */
puts( message );
printf("\n\n");
printf("You have three options!\n1) Manually Replace one encrypted letter with a letter of your choice (type 1)\n 2)\
Perform auto-guess which will use freq analysis to try and improve the encrpted file (type 2)\n3)End the Game\
(type 3)\n Enter your choice: ");
scanf("%d",i);
switch(i)
{
case 1: manual( key , message , j );
break;
case 2: auto_guess( key , message , freq );
break;
case 3: end_game( message );
break;
default: printf("Invalid choice");
break;
}
}
i = getchar();
return 0;
}
void manual( struct crypt key[] , char *message , int n )
{
char ch, kiwi;
printf("With manual replace you may choose an encrypted letter and the letter you want to decode it to.\nPlease enter \
the encrypted letter for which you want to change the decoded letter(lower-case): ");
scanf(" %c",ch);
printf("Now enter the letter you want your choice to decode to(lower-case): ");
scanf(" %c", kiwi);
return;
}