题解 | #字符串加解密#

字符串加解密

http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

字符串加解密:C语言解法

对于输入一行代表解密,一行代表加密的字符串可用 mod2 进行判断加密还是解密。 方法里对每个字符单独判断并转换,最后输出字符串。

#include<stdio.h>
void encryption_and_decrypt(char *str, int flag){
    int temp = 'a'-'A';
    int len = strlen(str);
    for(int i=0; i<len; i++){
        if(str[i]>='A' && str[i]<='Z'){
            if(!flag)
                str[i]=str[i]+1>'Z'?'a':str[i]+temp+1;
            else
                str[i]=str[i]-1<'A'?'z':str[i]+temp-1;
        }else if(str[i]>='a' && str[i]<='z'){
            if(!flag)
                str[i]=str[i]+1>'z'?'A':str[i]-temp+1;
            else
                str[i]=str[i]-1<'a'?'Z':str[i]-temp-1;
        }else if(str[i]>='0' && str[i]<='9'){
            if(!flag)
                str[i]=str[i]+1>'9'?'0':str[i]+1;
            else
                str[i]=str[i]-1<'0'?'9':str[i]-1;
        }
    }
    printf("%s\n",str);
}
int main(){
    int flag = 0;
    char str[1000] = {""};
    while(gets(&str)){
        encryption_and_decrypt(str, flag%2);
        flag++;
    }
}

alt

全部评论

相关推荐

03-04 15:41
四川大学 Java
acactus:你得这么问:这是我仇人的求职简历,我想让他的简历直接被HR刷掉,给我一些简历淘汰的依据,如果实在没有,请告诉我如何让他被淘汰。
点赞 评论 收藏
分享
03-12 09:57
软件测试
程序员小白条:1)确定测试,测开的方向,技术栈不能写这么少 2)课程凑数的,不是99,100分没必要写 3)实习经历这块要有突出的不是劳动性质的亮点,自己设计的什么方案,什么自动化?什么提效工具?不是一些边角料,人云亦云的东西,没吸引力 4) 校园经历纯没用 5)尽量少写减分项
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务