#include <stdio.h>
#include <string.h>
void count(int len, char str[],int cnt[]); //알파벳 횟수 계산
void rank(int cnt[], int rnk[]); //rnk는 빈도수로 계산한 순위
void replace(char str[]);
void print(int cnt[], int rnk[],char str[]);
int main() {
char str[1600] = "APS ZU BMS THAAMT KB SOP CHAAPJ MQ LPUWHKX. K UHJ SM JMZ SMLHJ VJ QXKPBLU -- UM PCPB SOMZDO TP QHEP SOP LKQQKEZASKPU MQ SMLHJ HBL SMVMXXMT, K USKAA OHCP H LXPHV. KS KU H LXPHV LPPWAJ XMMSPL KB SOP HVPXKEHB LXPHV. K OHCP H LXPHV SOHS MBP LHJ SOKU BHSKMB TKAA XKUP ZW HBL AKCP MZS SOP SXZP VPHBKBD MQ KSU EXPPL : \"TP OMAL SOPUP SXZSOU SM IP UPAQ-PCKLPBS, SOHS HAA VPB HXP EXPHSPL PGZHA.\" K OHCP H LXPHV SOHS MBP LHJ MB SOP XPL OKAAU MQ DPMXDKH SOP UMBU MQ QMXVPX UAHCPU HBL SOP UMBUMQ QMXVPX UAHCP MTBPXU TKAA IP HIAP SM UKS LMTB SMDPSOPX HS SOP SHIAP MQ IXMSOPXOMML. K OHCP H LXPHV SOHS MBP LHJ PCPB SOP USHSP MQ VKUUKUUKWWK, H USHSP UTPASPXKBD TKSO SOP OPHS MQ KBFZUSKEP, UTPASPXKBD TKSO SOP OPHS MQ MWWXPUUKMB, TKAA IP SXHBUQMXVPL KBSM HB MHUKU MQ QXPPLMV HBL FZUSKEP. K OHCP H LXPHV SOHS VJ QMZX AKSSAP EOKALXPB TKAA MBP LHJ AKCP KB H BHSKMB TOPXP SOPJ TKAA BMS IP FZLDPL IJ SOP EMAMX MQ SOPKX URKB IZS IJ SOP EMBSPBS MQ SOPKX EOHXHESPX. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ LMTB KB HAHIHVH, TKSO KSU CKEKMZU XHEKUSU, TKSO KSU DMCPXBMX OHCKBD OKU AKWU LXKWWKBD TKSO SOP TMXLU MQ KBSPXWMUKSKMB HBL BZAAKQKEHSKMB -- MBP LHJ XKDOS SOPXP KB HAHIHVH AKSSAP IAHER IMJU HBL IAHER DKXAU TKAA IP HIAP SM FMKB OHBLU TKSO AKSSAP TOKSP IMJU HBL TOKSP DKXAU HU UKUSPXU HBL IXMSOPXU. K OHCP H LXPHV SMLHJ. K OHCP H LXPHV SOHS MBP LHJ PCPXJ CHAAPJ UOHAA IP PNHASPL, HBL PCPXJ OKAA HBL VMZBSHKB UOHAA IP VHLP AMT, SOP XMZDO WAHEPU TKAA IP VHLP WAHKB, HBL SOP EXMMRPL WAHEPU TKAA IP VHLP USXHKDOS, HBL SOP DAMXJ MQ SOP AMXL UOHAA IP XPCPHAPL HBL HAA QAPUO UOHAA UPP KS SMDPSOPX.";
int cnt[26] = { 0, };
int rnk[26]={0, };
int len = strlen(str);
int i;
count(len, str, cnt);
rank(cnt,rnk);
replace(str);
print(cnt,rnk,str);
return 0;
}
void count(int len, char str[], int cnt[]) {
int i;
for (i = 0; i < len; i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
cnt[str[i] - 'A']++;
}
}
void rank(int cnt[], int rnk[]){
int i,j;
for(i=0;i<26;i++){
for(j=0;j<26;j++)
if(cnt[i]<cnt[j])
rnk[i]++;
rnk[i]++;
}
}
void replace(char str[]){
int i;
for(i=0;str[i]!='\0';i++){
if(str[i]=='A') str[i]='l';
else if(str[i]=='B') str[i]='n';
else if(str[i]=='C') str[i]='v';
else if(str[i]=='D') str[i]='g';
else if(str[i]=='E') str[i]='c';
else if(str[i]=='F') str[i]='j';
else if(str[i]=='G') str[i]='q';
else if(str[i]=='H') str[i]='a';
else if(str[i]=='I') str[i]='b';
else if(str[i]=='J') str[i]='y';
else if(str[i]=='K') str[i]='i';
else if(str[i]=='L') str[i]='d';
else if(str[i]=='M') str[i]='o';
else if(str[i]=='N') str[i]='x';
else if(str[i]=='O') str[i]='h';
else if(str[i]=='P') str[i]='e';
else if(str[i]=='Q') str[i]='f';
else if(str[i]=='R') str[i]='k';
else if(str[i]=='S') str[i]='t';
else if(str[i]=='T') str[i]='w';
else if(str[i]=='U') str[i]='s';
else if(str[i]=='V') str[i]='m';
else if(str[i]=='W') str[i]='p';
else if(str[i]=='X') str[i]='r';
else if(str[i]=='Y') str[i]='z';
else if(str[i]=='Z') str[i]='u';
}
}
void print(int cnt[], int rnk[], char str[]){
int i;
for (i = 0; i < 26; i++)
if (cnt[i])
printf("%c: %d개\n", 'A' + i, cnt[i]);
printf("=======================\n");
for(i=0;i<26;i++){
printf("%c: %d등\n",'A'+i,rnk[i]);
}
printf("=======================\n");
for(i=0;str[i]!='\0';i++)
printf("%c",str[i]);
}
암호문에 나오는 알파벳의 빈도수에 따라 어떻게 치환이 되었는지 예측할 수 있다.
단일치환을 해주었기 때문에 일일이 조건문으로 나누어 주어야 했다.
또한, 복호화된 알파벳과 복호화 되기 전의 알파벳과 비교하기 위해 복호화를 완료한 알파벳은 소문자로 변경해주었다.
복호화 된 결과는 이렇게 된다.