字符串的简单加密与解密 ...

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">      字符串的加密与解密,加密的方式是将字符串中每个字符加上它在字符串中的位置和一个偏移值5。如"mrsoft",第一个字符"m"在字符串中的位置为0,那么它对应的密文是“‘m’+0+5”,即r。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string s;
    getline(cin, s);

    for(int i = 0; i < s.size(); i++)
    {
        int temp = (i + 5) % 26;//判断循环了多少轮余多少次

        if((s[i] + temp <= 'z' && s[i] >= 'a') || (s[i] + temp <= 'Z' && s[i] >= 'A'))
        {
            s[i] = (char)(s[i] + temp);
        }
        else
        {
            s[i] = (char)(s[i] - 26 + temp);
        }
    }

    cout << s << endl;

    for(int i = 0; i < s.size(); i++)
    {
        int temp = (i + 5) % 26;

        if((s[i] - temp >= 'a' && s[i] <= 'z') || (s[i] - temp >= 'A' && s[i] <= 'Z'))
            s[i] = (char)(s[i] - temp);
        else
            s[i] = (char)(s[i] + 26 - temp);
    }

    cout << s << endl;
    return 0;
}

 
 

转载于:https://www.cnblogs.com/tolic/p/7142284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值