pta 凯撒密码(C语言实现)

题目描述

为了防止信息被别人轻易窃取,需要把电码明文通过加密方式变换成为密文。输入一个以回车符为结束标志的字符串(少于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;
} 
  • 22
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值