我创建了一个程序,它将'word'和num参数作为移位数。例如,如果字符串为Caesar Cipher
且num为2,则输出应为Ecguct Ekrjgt
。但我希望标点符号,空格和大小写保持不变。我也不能添加一句话。一句话。我不被允许使用字符串。
#include<iostream>
using namespace std;
int main()
{
char name[50] = { '\0' };
int cipherKey, len;
cout << "Enter cipher key: ";
cin >> cipherKey;
cout << "Enter a message to encrypt: ";
cin >> name;
len = strlen(name); // this code just inputs the length of a word. after spaces it won't calculate.
for (int i = 0; i < len; i++)
{
char temp = name[i] + cipherKey;
cout << temp;
}
cout << "\n\n";
return 0;
}
答案 0 :(得分:1)
要忽略标点符号,我建议您在for loop
内添加if
来选择要保持不变的标点符号。例如:
for (int i = 0; i < len; i++)
{
if( name[i]==' ' || name[i]==',' || name[i]=='.' /*...etc...*/ )
cout << name[i];
else {
char temp = name[i] + cipherKey;
cout << temp;
}
}
为每个特殊的标点符号键入它是一件单调乏味的事,但是c ++有点像:P
要使密码正常工作:如评论中所述,行
char temp = name[i] + cipherKey;
并不总能达到您的期望。具体来说,如果name[i]+cipherkey
不在字母表的末尾,您将需要回滚。我会留下那个让你弄明白的。 (提示:谷歌'ASCII值'以查看char
如何以数字形式表示)