/*功能:凯撒密码日期:2013-06-18*/#include<stdio.h>#include<stdlib.h>#include <string.h>void countFrequency(char *ch,int *frequency);int main(void){ char Ch[10000]; int Frequency[26]={0};
printf("请输入密文:n"); gets(Ch);
countFrequency(&Ch[0],&Frequency[0]);
printf("n"); system("pause"); return 0;}
/*************************************************************************函数名:countFrequency功能:统计英文字符出现的频率参数:char *ch 要统计字符 int *frequency 存放每个字符的出现频率的数组返回值:无*************************************************************************/
void countFrequency(char *ch,int *frequency){ int i,j,len,num,max; len = strlen(ch);
//计算并存放26个字母出现的频率 for (i = 0 ; i < 26 ;i++) { num = 0; for (j=0;j<len;j++) { if (*(ch+j) == 65+i || *(ch+j)== 97+i) { num++; } } *(frequency+i) = num; }//寻找出现频率最大字母 max = frequency[0];
for (i = 0 ; i < 26 ; i++) { if (max < frequency[i]) { j=i; max = frequency[i]; } }//得出位移位数 num=97+j-101;//字母移动 for (i = 0 ;i < len ; i++ ) { if(ch[i] >= 97 && ch[i] <= 99 || ch[i] >= 65 && ch[i] <= 67) { ch[i] = ch[i] + 26 - num ; } else if((ch[i] >=68 && ch[i]<=90)||(ch[i]>=100 && ch[i] <= 122)) {
ch[i] = ch[i] - num;
} } printf("解密后为:n"); puts(ch);
}凯撒密码

最后更新于 2022-12-25
部分内容可能已过时,请注意信息时效。
本文采用 CC BY-NC-SA 4.0 许可,转载请注明出处与作者。
支持一下
如果这篇文章对你有帮助,欢迎打赏或分享。
评论