题目描述
为了防止信息被别人轻易窃取,需要把电码明文通过加密方式变换成为密文。输入一个以回车符为结束标志的字符串(少于80个字符),再输入一个整数offset,用凯撒密码将其加密后输出。恺撒密码是一种简单的替换加密技术,将明文中的所有字母都在字母表上偏移offset位后被替换成密文,当offset大于零时,表示向后偏移;当offset小于零时,表示向前偏移。
输入格式:
输入第一行给出一个以回车结束的非空字符串(少于80个字符);第二行输入一个整数offset。
输出格式:
输出加密后的结果字符串。
解题思路
输入部分:输入一个字符串s和一个整数offset
数据处理过程:只对字符串s中的字母进行处理,判断偏移offset位之后是哪个字母。
输出部分:输出字符串s
两个测试点没有通过o(╥﹏╥)o生气!
这两个测试点通过了,原因在于offset要对26取余,否则可能超出ASCII码表的范围,而不是偏移之后再取余,这一点一定一定要注意哦!取余!取余!取余!
实现代码
#include <stdio.h>
int main()
{
// input
char s[80];
gets(s);
int offset;
scanf("%d", &offset);
// process
for (int i=0; s[i]!='\0'; i++){
if (s[i] >= 'a' && s[i] <= 'z'){
if (s[i] + offset % 26 > 'z')
s[i] = s[i] + offset % 26 - 'z' + 'a' - 1;
else if (s[i] + offset % 26 < 'a')
s[i] = s[i] + offset % 26 - 'a' + 'z' + 1;
else
s[i] = s[i] + offset % 26;
}
if (s[i] >= 'A' && s[i] <= 'Z'){
if (s[i] + offset % 26 > 'Z')
s[i] = s[i] + offset % 26 - 'Z' + 'A' - 1;
else if (s[i] + offset % 26 < 'A')
s[i] = s[i] + offset % 26 - 'A' + 'Z' + 1;
else
s[i] = s[i] + offset % 26;
}
}
// output
printf("%s\n", s);
return 0;
}
之后在做这道题,发现测试点2未通过,提示是全部是字母,想不明白是为什么。
#include <stdio.h>
#include <string.h>
int main(){
char str[85];
int offset, i = 0;
while ((str[i++]=getchar()) != '\n');
str[i] = '\0';
scanf("%d", &offset);
offset %= 26;
for (i=0; i<strlen(str); i++){
if (str[i]>='a' && str[i]<='z'){
str[i] += offset;
if (str[i] > 'z'){
str[i] = str[i] - 'z' + 'a' - 1;
}
if (str[i] < 'a'){
str[i] = str[i] - 'a' + 'z' + 1;
}
}
if (str[i]>='A' && str[i]<='Z'){
str[i] += offset;
if (str[i] > 'Z'){
str[i] = str[i] - 'Z' + 'A' - 1;
}
if (str[i] < 'A'){
str[i] = str[i] - 'A' + 'Z' + 1;
}
}
putchar(str[i]);
}
return 0;
}
修改之后的代码如下:
#include <stdio.h>
#include <string.h>
int main(){
char str[85];
int offset, i = 0;
while ((str[i++]=getchar()) != '\n');
str[i] = '\0';
scanf("%d", &offset);
offset %= 26;
for (i=0; i<strlen(str); i++){
if (str[i]>='a' && str[i]<='z'){
/* str[i] += offset;
// str[i] = (str[i] + offset) % 26 + 'a';
if (str[i] > 'z'){
str[i] = str[i] - 'z' + 'a' - 1;
}
if (str[i] < 'a'){
str[i] = str[i] - 'a' + 'z' + 1;
}
*/
str[i] = (str[i] - 'a' + offset + 26) % 26 + 'a';
}
if (str[i]>='A' && str[i]<='Z'){
/* str[i] += offset;
if (str[i] > 'Z'){
str[i] = str[i] - 'Z' + 'A' - 1;
}
if (str[i] < 'A'){
str[i] = str[i] - 'A' + 'Z' + 1;
}
*/
str[i] = (str[i] - 'A' + offset + 26) % 26 + 'A';
}
putchar(str[i]);
}
return 0;
}
【实现代码】
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[80];
printf("Enter a string:");
gets(str);
int offset;
printf("Enter offset:");
scanf("%d", &offset);
offset %= 26; //对offset取模
for (int i=0; str[i]; i++) {
if (isalpha(str[i])) {
if ((str[i]+offset>='a'&&str[i]+offset<='z') ||
(str[i]+offset<='Z'&&str[i]+offset>='A')) { //一般情况
str[i] += offset;
} else if (isupper(str[i]) && str[i]+offset>'Z') { //考虑特殊情况
str[i] = str[i] + offset - 'Z' + 'A' - 1;
} else if (islower(str[i]) && str[i]+offset>'z') {
str[i] = str[i] + offset - 'z' + 'a' - 1;
} else if (islower(str[i]) && str[i]+offset<'a') {
str[i] = str[i] + offset - 'a' + 'z' + 1;
} else if (isupper(str[i]) && str[i]+offset<'A') {
str[i] = str[i] + offset - 'A' + 'Z' + 1;
}
}
}
printf("After being encrypted:%s\n", str);
return 0;
}