#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INVALID_INPUT 100
#define INVALID_LENGTH 101
#define OUT_OF_MEMORY 105
#define SUCCESS 0
#define MEMORY_CHUNK 10
#define LETTER_COUNT 52
const char aZ[LETTER_COUNT] = {'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',
'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'};
char* get_string();
int compare(const char *str1, const char *str2);
void shift(const char *src, char *dst, int offset);
void copy_string(const char*, char*);
int main(){
char* encoded = get_string();
char* part_decoded = get_string();
int input_length = strlen(encoded);
if(input_length != strlen(part_decoded)){
fprintf(stderr, "Error: Chybna delka vstupu!n");
free(encoded);
free(part_decoded);
return INVALID_LENGTH;
}
char* shifted = malloc(sizeof(char)*input_length);
char* decoded = malloc(sizeof(char)*input_length);
int max_common = 0, common = 0;
for(int i = 0; i < LETTER_COUNT; i++){
shift(encoded, shifted, i);
common = compare(shifted, part_decoded);
if(common > max_common){
copy_string(shifted, decoded);
max_common = common;
}
}
free(encoded);
free(part_decoded);
free(shifted);
free(decoded);
return SUCCESS;
}
char* get_string(){
char* input;
int used_space = 0;
if((input = calloc(MEMORY_CHUNK, sizeof(char))) == null){
fprintf(stderr, "Error: Not enough memory!n");
free(input);
exit(OUT_OF_MEMORY);
}
char in;
int scan_retval = scanf("%c", &in);
while(in != 'n'){
if(scan_retval != 1 || strchr(aZ, in) == null){
fprintf(stderr, "Error: Chybny vstup!n");
free(input);
exit(INVALID_INPUT);
}
if(used_space + 1 > MEMORY_CHUNK){
if((input = realloc(input, (used_space + MEMORY_CHUNK) * sizeof(char))) == null){
fprintf(stderr, "Error: Not enough memory!n");
free(input);
exit(OUT_OF_MEMORY);
}
}
input[used_space++] = in;
scan_retval = scanf("%c", &in);
}
input[used_space] = ' ';
return input;
}
int compare(const char *str1, const char *str2){
int common = 0;
int length = strlen(str1);
for(int i = 0; i < length; i++){
if(str1[i] == str2[i]) common++;
}
return common;
}
void shift(const char *src, char *dst, int offset){
int length = strlen(src);
for(int i = 0; i < length; i++){
dst[i] = aZ[(strchr(aZ, src[i]) - aZ + offset) % LETTER_COUNT];
}
dst[length] = ' ';
}
void copy_string(const char* src, char* dst){
int length = strlen(src);
for(int i = 0; i < length; i++){
dst[i] = src[i];
}
}