家庭作业(修订):凯撒密码,当char是>'z'(c ++)时,环绕 [英] Homework(revision): Caesar cipher, wrap around when char's are >'z' (c++)
问题描述
有人可以给我一个提示,当角色到达时字母表的结尾。当然,我不希望有人为我做我的工作。
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是>'z'(c ++)时,环绕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!