家庭作业(修订):凯撒密码,当char是>'z'(c ++)时,环绕 [英] Homework(revision): Caesar cipher, wrap around when char's are >'z' (c++)

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

问题描述

我刚才在这里,手中添加了charci ascii引用来增加每个字母的密码转换。然而,我不知道如何解决字符高于'z'的问题。



有人可以给我一个提示,当角色到达时字母表的结尾。当然,我不希望有人为我做我的工作。

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

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<<< 无法读取文件<< endl;}
cout<< ENDL;
system(pause);
}


解决方案

模运算是你的朋友。每当整数形成环而不是序列时,可以应用模运算,如:

  5%4 == 17% 4 

您需要在相对于'a' ,当然可以适当地减去'a'


I was here earlier and got a hand adding to chars 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

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

这篇关于家庭作业(修订):凯撒密码,当char是&gt;'z'(c ++)时,环绕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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