您好我正在开发加密程序。该程序应采用用户输入的字母,并将其替换为字母表下方的相应字母11位。例如,如果用户输入" joe"该程序应输出" uzp"。
我当前的代码做得很好,但它不能识别空格,程序应该包裹字母表。因此,' Y'成为' J'和' Z'会成为' K'等任何人都知道如何解决这个问题?
void encrypt(std::string &e);
int main() {
string nameAttempt;
cout << "Enter your name to be Encrypted: ";
cin >> nameAttempt;
cout << "Original string is: " << nameAttempt << endl;
encrypt( nameAttempt );
cout << "Encrypted string is: " << nameAttempt << endl;
system("pause");
return 0;
}
void encrypt (std::string &e) {
const char* tempCharArray = e.c_str();
for( int i=0; i<e.size(); ++i )
e[i] = tempCharArray[i]+11;
} //
答案 0 :(得分:1)
假设你想:
void encrypt (std::string &e)
{
int size = e.size();
for (int i=0; i<size; i++)
{
char c = e[i];
if (('A' <= c && c <= 'Z'-11) || ('a' <= c && c <= 'z'-11))
e[i] = c+11;
else if ('Z'-11 < c && c <= 'Z')
e[i] = c+11-'Z'+'A';
else if ('z'-11 < c && c <= 'z')
e[i] = c+11-'z'+'a';
}
}
答案 1 :(得分:0)
你可以这样做:
char _character='X';
int _value=static_cast<int>(_character);
if(_value!=32)//not space
{
int _newValue=((_value+11)%90);
(_newValue<65)?_newValue+=65:_newValue+=0;
char _newCharacter=static_cast<char>(_newValue);
}