题解 | #字符串加解密#

字符串加解密

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

全部评论

相关推荐

字节一直是我的白月光,考虑到转正还是拒了日常实习。
从明天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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