[英]alphabet encryption program
嗨,我正在開發一個加密程序。 該程序應接受用戶輸入的字母,並用字母再下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;
} //
假設您要:
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';
}
}
您可以執行以下操作:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.