凯撒密码,当char为'z'时环绕 [英] Caesar cipher, wrap around when char's are >'z'

查看:62
本文介绍了凯撒密码,当char为'z'时环绕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候在这里,他得到了帮助,将字符添加到ASCII参考中来增加每个字母的密码移位.但是我不知道如何解决字符高于'z'的问题.

I was here earlier and got a hand adding to characters ASCII references to increment the cipher shift on each letter. However I have no idea how to fix the problem of characters being higher than 'z'.

有人可以给我一个提示,提示字符在到达字母末尾时如何回绕.我当然不希望有人为我做我的工作.

Can someone give me a hint towards how to wrap around when the characters reach the end of the alphabet. I don't expect anyone to do my work for me, of course.

char decrypt(char letter)
{
int increment = 9;
if(letter == ' ')
{
    return letter;
}
letter += increment;
return letter;
}

int main()
{
char message[446]; int i = 0; char space = ' ';
ifstream in("encryptedText.txt");
if(in.getline(message, 446))
{
    while(message[i])
    {
            cout << decrypt(tolower(message[i])) << endl;
            i++;
    }
}
else
{cout << "Can't read file" << endl;}
cout << endl;  
system("pause");
}

推荐答案

取模运算是您的朋友.每当整数形成环而不是序列时,都可以应用取模运算,例如:

The modulo operation is your friend. Whenever integers form a ring instead of a sequence, a modulo operation can be applied, like:

5 % 4 == 17 % 4

您需要在相对于'a'的整数空间中进行计算,当然要适当地减去'a'.

You need to compute this in the integer space relative to 'a', of course, subtracting 'a' appropriately.

这篇关于凯撒密码,当char为'z'时环绕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆