#include #include const int GRILL_SIZE = 6; const char HOLE = 'X'; FILE *open_file(const char *filename, const char *access); void read_matrix(const char *filename, char matrix[][GRILL_SIZE], int size); void decrypt(char output[], const char encrypted[][GRILL_SIZE], char grill[][GRILL_SIZE], int size); void turn_grill(char grill[GRILL_SIZE][GRILL_SIZE]); void write_output(const char *filename, char output[], int size); int main(int argc, char *argv[]) { if(argc != 4) { fprintf(stderr,"Invalid number of parameters, usage : charcnt \n"); exit(-1); } char encrypted[GRILL_SIZE][GRILL_SIZE]; char grill[GRILL_SIZE][GRILL_SIZE]; char plain[GRILL_SIZE*GRILL_SIZE]; read_matrix(argv[1], encrypted, GRILL_SIZE); read_matrix(argv[2], grill, GRILL_SIZE); decrypt(plain, encrypted, grill, GRILL_SIZE); write_output(argv[3], plain, GRILL_SIZE*GRILL_SIZE); return 0; } FILE *open_file(const char *filename, const char *access) { FILE *file_handle; file_handle = fopen(filename, access); if(file_handle == NULL) { fprintf(stderr,"Unable to open file %s\n", filename); exit(-1); } return file_handle; } void read_matrix(const char *filename, char matrix[][GRILL_SIZE], int size) { FILE *file_handle = open_file(filename, "r"); for(int i = 0; i < size ; i++) { for(int j = 0 ; j < size ; j++) matrix[i][j] = fgetc(file_handle); fgetc(file_handle); // The newline character } fclose(file_handle); } void decrypt(char output[], const char encrypted[][GRILL_SIZE], char grill[][GRILL_SIZE], int size) { int output_cnt = 0; for(int turns = 0 ; turns < 4 ; turns++) { for(int i = 0 ; i < size ; i++) for(int j = 0 ; j < size ; j++) { if(grill[i][j] == HOLE) output[output_cnt++] = encrypted[i][j]; } turn_grill(grill); } } void turn_grill(char grill[GRILL_SIZE][GRILL_SIZE]) { char temp[GRILL_SIZE][GRILL_SIZE]; for(int i = 0 ; i < GRILL_SIZE ; i++) for(int j = 0 ; j < GRILL_SIZE ; j++) temp[j][(GRILL_SIZE - 1) - i] = grill[i][j]; for(i = 0 ; i < GRILL_SIZE ; i++) for(j = 0 ; j < GRILL_SIZE ; j++) grill[i][j] = temp[i][j]; } void write_output(const char *filename, char output[], int size) { FILE *file_handle = open_file(filename, "w"); for(int i = 0 ; i < size ; i++) fputc(output[i], file_handle); fclose(file_handle); } ----------------------The Input--------------------------------- SEITIR HTEINE CLESTE ACIEIW HTVEEA EDVEKR ---------------------The Grille to try-------------------------- _X_X_X ____X_ __X___ _X__X_ _____X ___X__